Tôi thấy một hành vi lạ với ListView của mình, điều mà tôi dường như không thể xoay xở.Chiều cao động cho hàng ListView trong android
Tôi có một ListView, có một số dữ liệu. Mỗi hàng chứa một hình ảnh và ba nhãn văn bản. Dưới đây là XML của tôi cho hàng (Tôi đang thổi phồng nó trong phương thức getView
của Adaptor):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="@+id/Icon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:padding="2dp"
android:scaleType="fitCenter"
android:contentDescription="@string/app_icon"/>
<TextView android:id="@+id/TxtName"
android:scrollHorizontally="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@android:color/black"
android:background="@color/list_bg1"
android:layout_weight="2"
android:padding="2dp"/>
<TextView android:id="@+id/TxtPackage"
android:scrollHorizontally="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:textColor="@android:color/black"
android:background="@color/list_bg2"
android:padding="2dp"/>
<TextView android:id="@+id/TxtSource"
android:scrollHorizontally="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="@color/list_bg3"
android:textColor="@android:color/black"
android:padding="2dp"/>
</LinearLayout>
Bây giờ, bất cứ điều gì đi vào các lĩnh vực văn bản có thể của bất kỳ chiều dài (từ một vài ký tự để 80-100 ký tự) và tôi muốn các nhãn chỉ quấn và chiều cao của hàng để mở rộng để phù hợp với chiều cao của các nhãn được bọc.
Tuy nhiên, các nhãn làm bọc ok, nhưng chiều cao của hàng không mở rộng, do đó phần dưới cùng của nhãn bị cắt. Dưới đây là phương pháp của tôi getView
:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SetInfo app = (SetInfo)getItem(position);
if(app == null)
return null;
LinearLayout row = (LinearLayout) (convertView == null
? LayoutInflater.from(context).inflate(R.layout.listitem, parent, false)
: convertView);
((TextView)row.findViewById(R.id.TxtName)).setText(app.getName());
((TextView)row.findViewById(R.id.TxtPackage)).setText(app.getPackage());
((TextView)row.findViewById(R.id.TxtSource)).setText(app.getSource());
if(app.getIcon() != null)
((ImageView)row.findViewById(R.id.Icon)).setImageDrawable(app.getIcon());
return row;
}
Làm thế nào tôi có thể có các hàng để có chiều cao khác nhau, điều chỉnh tự động để phù hợp với tất cả các nội dung?
Cách sử dụng tốt hơn android: maxLines = 1 trong Textview trong Bố cục của hàng Ngăn chặn nhiều hơn một dòng văn bản –
Bạn đã thử adujusting android: layout_weight to 1 hoặc something? –
@ Samamir: Tôi muốn đối diện chính xác: các nhãn PHẢI bọc, vì tôi cần tất cả văn bản để hiển thị. –