Android 在xml中设置列表项行高无效
我尝试在xml文件中设置列表项行高。例如,我有一个listView和一个对应的listAdapter,该适配器从xml文件中膨胀视图:Android 在xml中设置列表项行高无效,android,xml,Android,Xml,我尝试在xml文件中设置列表项行高。例如,我有一个listView和一个对应的listAdapter,该适配器从xml文件中膨胀视图: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" andro
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_right"/>
<TextView
android:id="@+id/info"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="20dp"
android:textSize="16sp"
android:textColor="#f96f00"
android:layout_gravity="center"
android:lines="1"
android:ellipsize="end"
/>
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="12sp"
android:paddingRight="10dp"
/>
</LinearLayout>
但是,无论您设置了多大的高度,此
60dp
都没有效果。膨胀的行视图具有固定的高度。当我在布局内调整textview时,行高度已更改。为什么会发生这种情况,我如何将列表项行的高度指定给一个固定的度量?好吧,不必惊慌
在适配器的getView中,只需在返回视图之前编写以下代码
// Just reset the layout parms before return
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
60*3); // put proper dp pixel conversion here, in hdpi it 3px = 1dp
view.setLayoutParams(params);
return view;
膨胀行时,请将“parent”参数选中为null
View v = inflater.inflate(R.layout.filtered_trip_row, null);
相反,试试这个
View v = inflater.inflate(R.layout.filtered_trip_row, parent);
现在尝试在getView中设置您的高度
希望这对您有所帮助。我已经查看了LayoutFalter的infalte函数的源代码,如果根为null,则不会为该视图生成LayoutParams,并且在附加到其他视图时将使用默认的LayoutParams。如果设置了root,则充气机将为此视图生成并设置LayouParams。但是,无法将attachToRoot设置为true,因为视图get将很快添加到适配器视图中
View v = inflater.inflate(R.layout.filtered_trip_row, parent,false);
这就解决了我的问题。将列表视图的代码也发布到你的线性布局高度android:Layout\u Height=“match\u parent”请尝试将你的代码粘贴到我的eclipse中,并将我自己的图像替换为你的图像,它占据了60dp的高度,看起来不错,所以请检查图像的大小和列表适配器的邮政编码。您可以在textview中将布局权重指定为1。有关更多信息,请在此处输入您尝试的代码。您的xml look fine我已经查看了LayoutInfalter的infalte函数的源代码,如果根为null,则不会为此视图生成LayoutParams,并且在附加到其他视图时将使用默认的LayoutParams。如果设置了root,则充气机将为此视图生成并设置LayouParams。但是,您的回答部分正确,因为在listAdapter的get视图中,您无法将attachToRoot设置为true(设置root时默认为true),它将引发异常您可以创建LayoutParam的实例并设置为您的视图当我按照您的建议将“null”设置为“parent”时,它使我的应用程序崩溃,并给了我一个
java.lang.UnsupportedOperationException:AdapterView中不支持addView(视图,布局参数)。以前,我也有同样的问题。我的列表行太高,但是在xml
中调整它没有任何作用。您的回答确实解决了我的问题,但我认为更好的方法可能是使用View v=inflater.inflate(R.layout.filtered\u trip\u row,parent,false);因此,在xml中设置的布局属性将生效。非常感谢,这修复了我的自定义列表视图中的行项目太高的问题。