4

線形レイアウトをscrolviewに追加しようとしていますが、これはコードがコンパイルするコードですが、新しいレイアウトが表示されません

これは元のレイアウトです(追加したい)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" 

android:layout_margin="15dp"
android:layout_marginTop="15dp">

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" 
     android:id="@+id/ViewHistoryImageLayout">

    <ImageView
        android:id="@+id/HistoryImage"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.76"
        android:gravity="center"
        android:padding="10dp"
        android:src="@drawable/upload" 
        android:contentDescription="@string/HistoryImage"/>

    <TextView
        android:id="@+id/TranslatedText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.12"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="@string/translateImageButton" />

</LinearLayout>

これは私が数回追加したいレイアウトです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/TranslationMenuLayout" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<RatingBar
    android:id="@+id/ratingBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numStars="5" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

新しいレイアウトを追加するためのJavaコードは次のとおりです。

setContentView(R.layout.activity_view_history_image);
ScrollView sv = new ScrollView(this);
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View ll = inflater.inflate(R.layout.translation_menu, null);
sv.addView(ll);

コードは正常にコンパイルされ、アプリは実行されていますが、.xmlファイルの1つに問題はありませんか?

tnx

4

3 に答える 3

5

インフレータを使用する必要があります、変更:

LinearLayout ll = new LinearLayout(R.layout.translation_menu);

LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View ll = inflater.inflate(R.layout.translation_menu, null);
sv.addView(ll);

は、対応するXMLファイルからオブジェクトをLayoutInflater作成します。View

于 2013-01-10T13:15:00.160 に答える
1
  LayoutInflater li = LayoutInflater.from(context);
  LinearLayout ll = (LinearLayout) li.inflate(R.layout.translation_menu, this)

これが正しい方法です。

于 2013-01-10T13:17:06.503 に答える
1

を使用しLayoutInflatorます。

LayoutInflaterクラスは、レイアウトXMLファイルを対応するViewオブジェクトにインスタンス化するために使用されます。

つまり、XMLファイルを入力として受け取り、そこからViewオブジェクトを構築します。

ScrollView sv = new ScrollView(this);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );   
LinearLayout ll = (LinearLayout) li.inflate(translation_menu, this);  
sv.addView(ll);
于 2013-01-10T13:22:48.803 に答える