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?
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? –