如何在Android中自定义ListView的高度? [英] How to customized the ListView height in android?

查看:134
本文介绍了如何在Android中自定义ListView的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的初学者.我想显示一个包含数字的自定义列表视图.我自定义了ListView大小(android:layout_height="50dp").但是,当我运行该应用程序时,它会在很大程度上显示未提供的大小.我不知道如何解决这个问题.请任何人帮助我.

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

推荐答案

我想修复列表单元格的高度

I want to fix the list cell 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 Size单位 sp 代替 pt .

Use TextView Size unit sp instead of pt .

这篇关于如何在Android中自定义ListView的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆