Tôi đang cố thêm một số RelativeLayouts được tạo động vào một LinearLayout bên trong một RelativeLayout, nằm bên trong một ScrollView. Khi tổng chiều cao của tất cả các chế độ xem vượt quá kích thước của màn hình điện thoại, tất cả các chế độ xem được hiển thị chính xác. Nhưng khi tổng kích thước của chế độ xem được thêm động không đủ để lấp đầy màn hình, chỉ phần tử RelativeLayout đầu tiên được hiển thị và các phần tử khác không được hiển thị trên màn hình. Tôi thực sự vô vọng và không hiểu tại sao.Tự động thêm các khung nhìn vào RelativeLayout bên trong ScrollView
Đây là đoạn mã để tự động cư quan điểm bên trong bố trí tuyến tính:
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);
}
Đây là 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>
Và đây là file xml cho hoạt động này:
<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>
Và ảnh chụp màn hình:
Nếu không có đủ bố trí: (KHÔNG ĐÚNG, cần phải thể hiện 3 comments)
Với đủ bố trí: (ONE ĐÚNG, màn hình được điền)
tôi chỉ cần hiển thị tất cả ba bình luận trong trường hợp đầu tiên:/Cảm ơn trước.
Tại sao không sử dụng ListView với đầu trang và chân trang? –
thực sự bình thường, tôi có nhiều lượt xem khác ở trên giống như danh sách: một số lần xem hình ảnh, nút, v.v. vì vậy đây không phải là trường hợp phổ biến, nhưng tôi cũng cần giải quyết tình huống này, vì tôi không thể dựa vào thực tế là mọi chế độ xem khác trong hoạt động này sẽ được khởi tạo. bất kỳ suy nghĩ nào? – ecem