How to customized the ListView height in android?

2020-04-20 03:31发布

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"). But when I run the app it display the size largely which am not given. I don't know how to fix this problem. Please anyone help me.

activity_topic_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.yuvi.secrectsforhappylife.TopicList">

    <ListView
        android:id="@+id/topiclist"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

titledesign.xml

<?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="50dp"
    android:background="@drawable/storylist">

    <TextView
        android:id="@+id/topictitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/title"
        android:textSize="12pt"
        android:textStyle="bold"
        android:textColor="@android:color/background_light" />
</RelativeLayout>

TopicList.java

public class TopicList extends AppCompatActivity {

    String topic[]={"one","two","three","four","five","six","seven","eight","nine","ten"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic_list);

        ListView listView=(ListView)findViewById(R.id.topiclist);

        CustomAdapter customAdapter=new CustomAdapter();
        listView.setAdapter(customAdapter);

    }

    class CustomAdapter extends BaseAdapter
    {

        @Override
        public int getCount() {
            return topic.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView=getLayoutInflater().inflate(R.layout.titledesign,null);
            TextView title=(TextView)convertView.findViewById(R.id.topictitle);
            title.setText(topic[position]);
            return convertView;
        }
    }
}

4条回答
一夜七次
2楼-- · 2020-04-20 03:38

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">
查看更多
相关推荐>>
3楼-- · 2020-04-20 03:55

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 .

查看更多
家丑人穷心不美
4楼-- · 2020-04-20 03:55

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();
查看更多
霸刀☆藐视天下
5楼-- · 2020-04-20 04:04

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);

查看更多
登录 后发表回答