簡體   English   中英

如何設置自定義列表視圖項目的高度?

[英]How to set custom list view item's height?

對此已經有很多疑問,但是沒人能幫助我。 我已經在我的類適配器中使用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則使您的@dimen/note_item_heigth共享給定的高度:

<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" >

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM