2010-04-29 38 views
5

Tôi hiện đang làm việc trên một bản cập nhật nhỏ cho một dự án và tôi đang gặp sự cố với Relative_Layout và fill_parent trong chế độ xem danh sách. Tôi đang cố gắng để chèn một chia giữa hai phần trong mỗi hàng, giống như chia trong nhật ký cuộc gọi của trình quay số mặc định. Tôi đã kiểm tra mã nguồn Android để xem họ đã làm như thế nào, nhưng tôi đã gặp sự cố khi sao chép giải pháp của họ.Android RelativeLayout fill_parent hành vi bất ngờ trong một ListView với chiều cao hàng khác nhau

Để bắt đầu, đây là bố cục mục hàng của tôi:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/RelativeLayout01" 
android:layout_width="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:padding="10dip" 
android:layout_height="fill_parent" 
android:maxHeight="64dip" 
android:minHeight="?android:attr/listPreferredItemHeight"> 
<ImageView android:id="@+id/infoimage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:src="@drawable/info_icon_big" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true"/> 
<View android:id="@+id/divider" 
    android:background="@drawable/divider_vertical_dark" 
    android:layout_marginLeft="11dip" 
    android:layout_toLeftOf="@+id/infoimage" 
    android:layout_width="1px" 
    android:layout_height="fill_parent" 
    android:layout_marginTop="5dip" 
    android:layout_marginBottom="5dip" 
    android:layout_marginRight="4dip"/> 
<TextView android:id="@+id/TextView01" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_toRightOf="@+id/ImageView01" 
    android:layout_toLeftOf="@+id/divider" 
    android:gravity="left|center_vertical" 
    android:layout_marginLeft="4dip" 
    android:layout_marginRight="4dip"/> 
<ImageView android:id="@+id/ImageView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:background="@drawable/bborder" 
    android:layout_centerVertical="true"/> 
</RelativeLayout> 

Vấn đề tôi phải đối mặt là mỗi hàng có một thu nhỏ của chiều cao khác nhau (ImageView01). Nếu tôi đặt thuộc tính layout_height của RelativeLayout thành fill_parent, bộ chia không chia tỷ lệ theo chiều dọc để lấp đầy hàng (nó chỉ là dấu chấm 1px). Nếu tôi đặt layout_height thành "? Android: attr/listPreferredItemHeight", dải phân cách sẽ lấp đầy hàng, nhưng hình thu nhỏ thu nhỏ lại. Tôi đã thực hiện một số gỡ lỗi trong phương thức getView() của bộ điều hợp, và có vẻ như chiều cao của bộ chia không được đặt đúng khi hàng có chiều cao thích hợp.

Đây là một phần của phương pháp getView():

public View getView(int position, View view, ViewGroup parent) { 
      if (view == null) { 
       view = inflater.inflate(R.layout.tag_list_item, parent, false); 
      } 

Phần còn lại của phương pháp này chỉ đơn giản là thiết lập văn bản và hình ảnh thích hợp cho hàng.

Ngoài ra, tôi tạo ra các đối tượng inflater trong constructor của adapter với: inflater = LayoutInflater.from(context);

Tôi có thiếu một cái gì đó quan trọng? Hay fill_parent không hoạt động với chiều cao động?

Trả lời

0

Tôi nghi ngờ phụ thuộc vòng tròn trong bố cục của bạn. Bạn có chắc chắn muốn hàng của layout_heightfill_parent? Có vẻ như bạn muốn hàng có chiều cao giống với chiều cao con lớn nhất (@id/ImageView01 trong trường hợp này), vì vậy bạn muốn layout_height="wrap_content" trong số RelativeLayout. Bằng cách này, chiều cao sẽ truyền từ hình ảnh đến hàng và sau đó xuống các trẻ khác có số layout_height="fill_parent".

+0

Cảm ơn bạn đã trả lời! Tuy nhiên, dải phân cách vẫn không lấp đầy hàng theo chiều dọc. Tôi không chắc chắn nếu hành vi này là một lỗi hoặc nếu có cái gì khác tôi đang mất tích. Bất kỳ ý tưởng nào khác? –

0

Đối với bất kỳ ai tò mò. Dường như điều này có thể là (hoặc đã từng) một lỗi trong SDK. Để khắc phục vấn đề, tôi phải đi với chiều cao cố định hàng. Đây là mã bố cục cuối cùng:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/RelativeLayout01" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="100dip" android:layout_width="fill_parent"> 
    <RelativeLayout android:id="@+id/Thumbnail" 
     android:layout_alignParentLeft="true" android:paddingLeft="10dip" 
     android:paddingTop="10dip" android:paddingRight="15dip" 
     android:paddingBottom="10dip" android:layout_height="fill_parent" 
     android:layout_width="wrap_content"> 
     <ImageView android:id="@+id/image" 
      android:layout_width="80dip" android:background="@drawable/bborder" 
      android:layout_centerVertical="true" android:adjustViewBounds="true" 
      android:layout_height="80dp" android:cropToPadding="true" /> 
    </RelativeLayout> 
    <View android:id="@+id/divider" android:background="@drawable/divider_light" 
     android:layout_toRightOf="@+id/Thumbnail" android:layout_marginTop="10dip" 
     android:layout_marginBottom="10dip" android:layout_width="1px" 
     android:layout_height="fill_parent" android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true" /> 
    <LinearLayout 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:paddingTop="10dp" android:paddingBottom="10dp" 
     android:layout_centerVertical="true" android:orientation="vertical" 
     android:layout_marginLeft="20dip" android:layout_marginRight="10dip" 
     android:layout_toRightOf="@+id/divider"> 
     <TextView android:id="@+id/title" android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:gravity="left|center_vertical" android:maxLines="1" android:singleLine="true" 
      android:ellipsize="end" /> 
     <TextView android:id="@+id/science_name" android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" 
      android:gravity="left|center_vertical" android:maxLines="1" android:singleLine="true" 
      android:ellipsize="end" android:textStyle="italic" /> 
    </LinearLayout> 
</RelativeLayout> 
6

Nếu bất kỳ ai khác gặp vấn đề này, cách duy nhất tôi tìm thấy (ngoài chiều cao cố định) là chuyển sang LinearLayout. Tôi đoán điều này là do sự khác biệt cơ bản về cách LinearLayouts được rút ra so với RelativeLayouts. Romain Guy nói về nó trong bài viết this. LinearLayouts gọi onMeasure() một lần cho chiều rộng và một lần cho chiều cao, nơi RelativeLayouts không, vì vậy họ không có cách nào để quay trở lại và điền vào một chia dọc theo chiều cao propper một khi mọi thứ khác đã được đặt ra. Giải pháp sau:

<LinearLayout xmlns:... 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    > 
    <RelativeLayout android:id="@+id/leftContent" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     > 
    </RelativeLayout> 
    <View android:id="@+id/divider" 
     android:layout_width="1px" 
     android:layout_height="fill_parent" 
     android:background="@drawable/divider" /> 
    <RelativeLayout android:id="@+id/rightContent" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     > 
    </RelativeLayout> 
</LinearLayout> 

Điều này sẽ mang lại cho bạn kết quả mong muốn, mặc dù nó không hiệu quả như chỉ một RelativeLayout.

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