我看到一個奇怪的行爲與我的ListView,我似乎無法解決。Android的ListView行的動態高度
我有一個ListView,填充了一些數據。每行包含一個圖像和三個文本標籤。這裏是我的XML爲行(我誇大它的適配器的getView
法):
<?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>
現在,無論進入文本字段可以是任何lenght的(從幾個字符以80-100字)並且我希望標籤只是包裝並且行的高度要展開以適應包裝標籤的高度。
然而,標籤不換行,但該行的高度不會擴大,因此標籤的底部被切斷。這裏是我的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;
}
怎樣纔可以有行是不同高度,自動調整,以適合所有內容?
更好的辦法採用Android:MAXLINES = 1的TextView行其防止更多然後一個文本行 – 2012-03-01 14:50:20
的佈局,你試過adujusting安卓layout_weight 1或東西嗎? – 2012-03-01 14:51:40
@Samir:我想要的是完全相反:標籤必須包裝,因爲我需要所有文本都可見。 – 2012-03-01 15:06:25