How to customized the ListView height in android?

前端 未结 4 1398
说谎
说谎 2021-01-24 08:41

I am a beginner to android. I want to display a customized list view which contains a number. I customized my ListView size(android:layout_height=\"50dp\"

相关标签:
4条回答
  • 2021-01-24 09:12

    Create the List view as activity_word.xml

    <?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="match_parent">
    
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:padding="5dp"
        android:textSize="25dp"
        android:gravity="center_horizontal|center_vertical"
        android:text="Text"
        android:id="@+id/text_list_view" />
    </LinearLayout>
    

    create another xml file and create a text view in it listitem.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:id="@+id/listView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>
     </RelativeLayout>
    

    in the Toplist java class add this as setContentView setContentView(R.layout.activity_word);

    0 讨论(0)
  • 2021-01-24 09:19

    Try below changes on your parent layout on titledesign.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/storylist">
    

    and if you want that your list capture full width of device screen make change on layout_width like

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/storylist">
    
    0 讨论(0)
  • 2021-01-24 09:23

    I want to fix the list cell height

    FYI

    You want to add HEIGHT for each Cell

    Then

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/storylist">
    
        <TextView
            android:id="@+id/topictitle"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="@string/title"
            android:textSize="12sp"
            android:textStyle="bold"
            android:textColor="@android:color/background_light" />
    
    </RelativeLayout>
    

    Note

    Use TextView Size unit sp instead of pt .

    0 讨论(0)
  • 2021-01-24 09:23

    This should help you out..

    ListAdapter listadp = lv_ancillaryservice.getAdapter();
       if (listadp != null) {
           int totalHeight = 0;
           for (int i = 0; i < listadp.getCount(); i++) {
               View listItem = listadp.getView(i, null, lv_ancillaryservice);
               listItem.measure(0, 0);
               totalHeight += listItem.getMeasuredHeight();
           }
           ViewGroup.LayoutParams params = lv_ancillaryservice.getLayoutParams();
           params.height = totalHeight + (lv_ancillaryservice.getDividerHeight() * (listadp.getCount() - 1));
           lv_ancillaryservice.setLayoutParams(params);
           lv_ancillaryservice.requestLayout();
    
    0 讨论(0)
提交回复
热议问题