如何设置自定义列表视图项的高度?
问题描述:
已经有很多关于这方面的问题,但没有人能帮助我。 我试过在我的班级适配器view.setMinimumHeigth(minHeigth)
,但没有奏效。 我试过View view = inflater.inflate(R.layout.list_note_item,parent);
,但该应用程序崩溃; 唯一足够接近的是这样的: View view = inflater.inflate(R.layout.list_note_item,parent,false);
但是然后项目有相同的高度,但他们只显示第一个文本视图,而不是第二个。 我该怎么做?如何设置自定义列表视图项的高度?
这里是我的LIST_ITEM XML布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/note_item_heigth"
android:orientation="vertical" >
<TextView
android:id="@+id/textview_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:background="#FFFFFF"
android:textSize="16sp"
/>
<TextView
android:id="@+id/textview_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#808080"
android:background="#FFFFFF"
android:textSize="14sp"
android:maxLength="80"
/>
和我的ListView XML布局:
<ListView
android:id="@+id/list_notes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@android:color/transparent"
android:dividerHeight="@dimen/space_between_items"
></ListView>
和我的课适配器
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_note_item,parent,false);
TextView title = (TextView)view.findViewById(R.id.textview_title);
TextView note = (TextView)view.findViewById(R.id.textview_note);
title.setText("test");
note.setText("TEST");
提前感谢!
答
如果您的LinearLayout必须使用@dimen/note_item_heigth
然后进行textviews共享给定的高度:
<TextView
android:id="@+id/textview_title"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textColor="#000000"
android:background="#FFFFFF"
android:textSize="16sp" />
<TextView
android:id="@+id/textview_note"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textColor="#808080"
android:background="#FFFFFF"
android:textSize="14sp"
android:maxLength="80" />
注意:您可能需要降低TEXTSIZE,为适应TextView的文本。
否则,如果你不关心列表项的高度,那么你可以在你的LinearAdapter设置为wrap_content
,并用它做:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
+0
太棒了!第一个解决方案奏效!谢谢 – Automatik 2014-09-10 18:50:35
你看看这个[文章](HTTP ://stackoverflow.com/a/7533367/3326331) – 2014-09-10 18:36:17