Я пытаюсь добавить динамически созданные несколько RelativeLayouts в LinearLayout, который находится внутри RelativeLayout, который находится внутри ScrollView. Когда общая высота всех просмотров превышает размер экрана телефона, все представления отображаются правильно. Но когда общий размер динамически добавленных просмотров недостаточно для заполнения экрана, отображается только первый элемент RelativeLayout, а остальные не отображаются на экране. Я действительно безнадежен и не понимаю, почему.Динамическое добавление просмотров в RelativeLayout внутри ScrollView
Вот код, динамически заполнять представления внутри линейной компоновки:
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 комментариев)
При наличии достаточного количества макетов: (правильный, экран заполнен)
I просто нужно показать все три комментария в первом случае:/Спасибо заранее.
Почему бы не использовать ListView с заголовком и нижним колонтитулом? –
На самом деле, как правило, у меня есть много других видов над теми, которые выглядят как список: несколько изображений, кнопок и т. Д., Так что это не обычный случай, но мне тоже нужно решить эту ситуацию, потому что я не могу полагаться на то, что каждый другой вид в этом действии будет инициализирован. Есть предположения? – ecem