8

ScrollView 内にある RelativeLayout 内にある LinearLayout に動的に作成されたいくつかの RelativeLayouts を追加しようとしています。すべてのビューの高さの合計が電話画面のサイズを超える場合、すべてのビューが正しく表示されます。ただし、動的に追加されたビューの合計サイズが画面いっぱいに収まらない場合、最初の RelativeLayout 要素のみが表示され、他の要素は画面に表示されません。私は本当に絶望的で、その理由がわかりません。

線形レイアウト内のビューを動的に設定するコードは次のとおりです。

LinearLayout commentsLayout = (LinearLayout) findViewById(R.id.comments_layout);

LayoutInflater inflater = (LayoutInflater)
    this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for(Comment c: commentsList) {

    RelativeLayout layoutItem = (RelativeLayout) inflater.inflate(
        R.layout.list_item_comment, null, false);

        TextView tv = (TextView) layoutItem.findViewById(R.id.textView);
        ImageView iv = (ImageView) layoutItem.findViewById(R.id.imageView);

        // set tv's text
        // set iv's image and onclicklistener, nothing fancy here, everything goes well

        commentsLayout.addView(layoutItem);
}

ここに list_item_comment.xml があります:

<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
>
    <ImageView 
    android:id="@+id/imageView"
    android:layout_width="50dip"
    android:layout_height="50dip"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    />

    <TextView 
    android:id="@+id/textView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:textSize="16sp"
    android:layout_toRightOf="@id/imageView"
    />
</RelativeLayout>

このアクティビティの xml ファイルは次のとおりです。

<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_layout"
>
...

    <ScrollView
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:id="@+id/scrollView"
    >

        <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/relativeContainer"
        >

        ...

            <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/comments_layout"
            />

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

そしてスクリーンショット:

十分なレイアウトがない場合: (不正解、3 つのコメントを表示する必要があります)

間違ったもの

十分なレイアウトがある場合: (正しいもの、画面がいっぱいです)

正しいもの

最初のケースで 3 つのコメントをすべて表示する必要があるだけです :/ よろしくお願いします。

4

1 に答える 1

0

の代わりに、のfill_parentをに変更してみてください。layout_height<RelativeLayout>list_item_comment.xmlwrap_content

また、なぜアクティビティのxml<RelativeLayout>内に別のものが必要なのですか。あなたがあなたの活動をどのように見せたいかをする<ScrollView>のに十分です。LinearLayout多分あなたはそれを取り除くことができます。

于 2012-04-04T17:01:18.200 に答える