如何在android中自定义ListView高度?

时间:2017-04-26 05:57:18

标签: android listview android-custom-view

我是android的初学者。我想显示一个包含数字的自定义列表视图。我自定义了ListView尺寸(android:layout_height="50dp")。但是当我运行应用程序时,它显示的大小基本上没有给出。我不知道如何解决这个问题。请有人帮助我。

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 个答案:

答案 0 :(得分:1)

  

我想修复列表单元格高度

<强> FYI

您想为每个单元格

添加 HEIGHT

然后

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

注意

使用TextView大小单位 sp 而不是 pt

答案 1 :(得分:0)

在titledesign.xml上的父布局上尝试以下更改

Invoke-RestMethod

如果您希望列表捕获设备屏幕的整个宽度,请在layout_width上进行更改,如

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

答案 2 :(得分:0)

这可以帮助你..

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

答案 3 :(得分:0)

创建列表视图 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>

创建另一个xml文件并在其中创建文本视图 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>
在Toplist java类中的

将其添加为setContentView 的setContentView(R.layout.activity_word);