我有一个包含在 HorizontalScrollView 中的 ListView,其中列表项视图根据内容具有不同的高度。列表项布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:weightSum="1" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:minHeight="50dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/nextStopType"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="P"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/dohrn_orange" />
<TextView
android:id="@+id/nextStopName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:maxLines="2"
android:text="Customer Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<TextView
android:id="@+id/nextstopAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="2"
android:paddingLeft="20dp"
android:text="Address"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:orientation="vertical" >
<TextView
android:id="@+id/nextStopCount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="@dimen/nextStopDetailsWidth"
android:text="Stop Count"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/nextStopTimes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="?android:attr/textAppearanceSmall"
android:text="Open/Close Times"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/nextStopContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact Number"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight=".2"
android:gravity="right" >
<TextView
android:id="@+id/nextStopPieces"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:minWidth="@dimen/nextStopPiecesWidth"
android:text="HUs"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/nextStopWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:minWidth="@dimen/nextStopWeightWidth"
android:text="Weight"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
如果 nextStopName 是单行,一切都很好,但如果它换成两行,则列表的高度计算错误。如果我查看 DDMS 中的视图层次结构,ListView 的高度是 111 像素,但包含列表项的 LinearLayout 的高度是 122 像素,这是包含 nextStopType 和 nextStopName 的 LinearLayout 的高度。111 实际上是包含 nextStopCount 等的第二个 LinearLayout 的高度。人。
基本上,每当 nextStopName 换行时,我都会收到此错误。
下面是一个屏幕截图,您可以在其中看到效果。
下面是这个屏幕的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:fillViewport="true"
android:layout_weight="1" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:gravity="center_vertical"
android:maxLines="2"
android:minWidth="@dimen/nextStopAddressWidth"
android:text="Customer Name & Address"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:hint="?android:attr/textAppearanceMedium"
android:minWidth="@dimen/nextStopDetailsWidth"
android:text="Details"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight=".2"
android:gravity="right" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:minWidth="@dimen/nextStopPiecesWidth"
android:text="HUs"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:minWidth="@dimen/nextStopWeightWidth"
android:text="Weight"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/eolMargin"
android:gravity="center"
android:text="Waiting For Stops"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/logoutButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/eolMargin"
android:text="Logout"
android:textAppearance="?android:attr/textAppearanceButton" />
</LinearLayout>
适配器的相关部分很简单。
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(rowLayout, parent, false);
}
StopItem stopItem = getItem(position);
TextView pdFlag = (TextView) row.findViewById(R.id.nextStopType);
pdFlag.setText(stopItem.getType());
if (isCurrentDestination(stopItem)) {
pdFlag.setTextColor(Color.GREEN);
} else {
pdFlag.setTextColor(DOHRN_ORANGE);
}
TextView name = (TextView) row.findViewById(R.id.nextStopName);
name.setText(stopItem.getCustomerName());
TextView address = (TextView) row.findViewById(R.id.nextstopAddress);
address.setText(stopItem.getAddress());
TextView stopCount = (TextView)row.findViewById(R.id.nextStopCount);
stopCount.setText(stopItem.getStopCount() );
TextView timesView = (TextView)row.findViewById(R.id.nextStopTimes);
String times = stopItem.getTimes("\n");
if (times.length() == 0) {
timesView.setVisibility(View.GONE);
} else {
timesView.setText(times);
}
TextView phoneView = (TextView)row.findViewById(R.id.nextStopContact);
String phoneNumber = stopItem.getFormattedPhoneNumber();
if (phoneNumber.length() == 0) {
phoneView.setVisibility(View.GONE);
} else {
phoneView.setText(phoneNumber);
}
TextView pieceCount = (TextView)row.findViewById(R.id.nextStopPieces);
pieceCount.setText(stopItem.getPieceCount());
TextView weight = (TextView)row.findViewById(R.id.nextStopWeight);
weight.setText(stopItem.getWeight());
return row;
}
我在布局上尝试了许多变化,结果都是一样的。奇怪的是,应用程序中还有其他 ListViews 具有不同高度的项目并且它们工作正常,但它们既没有包裹在 HorizontalScroll View 中,也没有那么复杂的项目布局。