8

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)

Incorrect one

Với đủ bố trí: (ONE ĐÚNG, màn hình được điền)

correct one

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.

+0

Tại sao không sử dụng ListView với đầu trang và chân trang? –

+0

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

Trả lời

0

thay vì fill_parent, hãy thử thay đổi layout_height của <RelativeLayout> trong số list_item_comment.xml thành wrap_content.

Ngoài ra, tại sao bạn cần một <RelativeLayout> khác trong số <ScrollView> của xml hoạt động của mình. Các LinearLayout là đủ để làm những gì bạn muốn hoạt động của bạn trông như thế nào. Có thể bạn chỉ cần loại bỏ nó.

+0

Tôi đã thử đề xuất của bạn nhưng có vẻ như không giải quyết được vấn đề:/Thực tế đây không phải là trường hợp phổ biến của hoạt động của tôi, thông thường phải có một 2 lần xem hình ảnh, một nút, một số bản xem trước v.v. do đó relativelayout giúp công việc của tôi dễ dàng hơn. Nhưng đây là một trường hợp quá (mặc dù nó là hiếm), do đó tôi nên tìm một giải pháp:/ – ecem

+0

@ecem, Bạn có thể tìm thấy một giải pháp cho điều này? – WindsurferOak

Các vấn đề liên quan