【问题标题】:How to customized the list item height in android? [duplicate]如何在android中自定义列表项高度? [复制]
【发布时间】:2017-04-26 08:49:43
【问题描述】:

我是安卓的初学者。我想显示一个包含数字的自定义列表视图。我自定义了我的列表项高度大小(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="match_parent"
    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="25sp"
        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;
            }
        }
    }

【问题讨论】:

    标签: android listview android-custom-view


    【解决方案1】:

    试试下面的代码

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

    并在您的列表视图中进行更改,如下所示

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

    【讨论】:

      猜你喜欢
      • 2012-06-18
      • 1970-01-01
      • 2011-04-05
      • 2011-05-03
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多