Android 如何设置ListView Rows Height

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10379727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:27:58  来源:igfitidea点击:

How to set the ListView Rows Height

androidandroid-layoutandroid-listviewbaseadapter

提问by user755278

I'm showing you the image below in which the height of the rows are not same i want the Rows should be of the same height.

我向您展示了下面的图像,其中行的高度不同,我希望行的高度相同。

enter image description here

在此处输入图片说明

In my program i'm getting the data from the XML service which i parsered and displaying the result on the list view but the height of the ListView Rows are not same.

在我的程序中,我从 XML 服务获取数据,我解析并在列表视图上显示结果,但 ListView 行的高度不同。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:background="@drawable/list_selector"
  android:padding="3dip"
  android:layout_width="match_parent"
  android:gravity="center"
  android:layout_height="10dp">

    <TextView android:layout_height="wrap_content" android:text="ITEM"
        android:layout_width="wrap_content" android:id="@+id/txtItem"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="15dip"
        android:textStyle="bold"
        android:textColor="#040404"></TextView>

    <TextView android:layout_height="wrap_content" android:text="MANUFACTURER"
        android:layout_width="wrap_content" android:id="@+id/txtItemTwo"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14dip"
        android:textColor="#FF7F50"

        ></TextView>


</LinearLayout>

I'm already sets the height

我已经设置了高度

android:layout_height="10dp"

android:layout_height="10dp"

but still not same when diplaying on listView.

但在 listView 上显示时仍然不一样。

This is my custom adapter for getVieew :-

这是我的 getView 自定义适配器:-

 public class MyEventAdapter extends BaseAdapter {

     ArrayList < String > listTitle;
     ArrayList < String > listFullText;

     Activity activity;

     public MyEventAdapter(Activity activity, ArrayList < String > listTitle, ArrayList < String > listFullText) {
         super();
         this.listTitle = listTitle;
         this.listFullText = listFullText;

         this.activity = activity;
     }

     public int getCount() {
         // TODO Auto-generated method stub
         return listTitle.size();
     }

     public Object getItem(int position) {
         // TODO Auto-generated method stub
         return null;
     }

     public long getItemId(int position) {
         // TODO Auto-generated method stub
         return 0;
     }

     private class ViewHolder {
         TextView txtViewTitle;
         TextView txtViewTitleTwo;

     }

     public View getView(int position, View view, ViewGroup parent) {
         // TODO Auto-generated method stub

         ViewHolder title;
         LayoutInflater inflater = activity.getLayoutInflater();

         if (view == null) {
             view = inflater.inflate(R.layout.lview_row, null);
             title = new ViewHolder();

             title.txtViewTitle = (TextView) view.findViewById(R.id.txtItem);
             title.txtViewTitleTwo = (TextView) view.findViewById(R.id.txtItemTwo);

             view.setTag(title);
         } else {
             title = (ViewHolder) view.getTag();
         }

         title.txtViewTitle.setText(listTitle.get(position));
         title.txtViewTitleTwo.setText(listFullText.get(position));

         return view;
     }
 }

Anyone please me the proper way to getting the desired out result.

任何人都请我以正确的方式获得所需的结果。

Thanks in advance.

提前致谢。

回答by clever

the problem is that you are mis-using the inflator, don't give null as the root node

问题是您误用了充气机,不要将 null 作为根节点

if you pass null as the root node, all the size parameters you set in the xml fail to work

如果您将 null 作为根节点传递,则您在 xml 中设置的所有大小参数都无法工作

the correct code is

正确的代码是

view = inflater.inflate(R.layout.lview_row, parent, false);

http://www.doubleencore.com/2013/05/layout-inflation-as-intended/

http://www.doubleencore.com/2013/05/layout-inflation-as-intended/

回答by Ravi1187342

override method getView(int position, View convertView, ViewGroup parent)in your MyEventAdapterclass and use view.setMinimumHeight(minHeight);to set minimum height of view.

重载方法getView(int position, View convertView, ViewGroup parent)在你的MyEventAdapter类和使用view.setMinimumHeight(minHeight);到的视图设置最小高度。

set minimum height of view with in method `getView(int position, View convertView, ViewGroup parent)' as following:

使用方法`getView(int position, View convertView, ViewGroup parent)' 设置最小视图高度,如下所示:

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder title;
    LayoutInflater inflater = activity.getLayoutInflater();

    if (view == null) {
        view = inflater.inflate(R.layout.lview_row, null);
        view.setMinimumHeight(minHeight); //set minimum height of view here
        title = new ViewHolder();

        title.txtViewTitle = (TextView) view.findViewById(R.id.txtItem);
        title.txtViewTitleTwo = (TextView) view.findViewById(R.id.txtItemTwo);

        view.setTag(title);
    } else {
        title = (ViewHolder) view.getTag();
    }

    title.txtViewTitle.setText(listTitle.get(position));
    title.txtViewTitleTwo.setText(listFullText.get(position));

    return view;    
}

回答by Bharat Sharma

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:background="@drawable/list_selector"
  android:padding="3dip"
  android:layout_width="match_parent"
  android:gravity="center"
  android:layout_height="match_parent">

    <TextView android:layout_height="wrap_content" android:text="ITEM"
        android:layout_width="wrap_content" android:id="@+id/txtItem"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textColor="#040404"></TextView>

    <TextView android:layout_height="wrap_content" android:text="MANUFACTURER"
        android:layout_width="wrap_content" android:id="@+id/txtItemTwo"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="14sp"
        android:textColor="#FF7F50"

        ></TextView>


</LinearLayout>

If you are having more than one line in your text then set your text as single line so that it will not come in second line and change your height of each row.

如果您的文本中有不止一行,则将您的文本设置为单行,这样它就不会出现在第二行并更改每行的高度。

回答by u5255288

If you are having more than one line in your text then set your text as single line so that it will not come in second line and change your height of each row.

如果您的文本中有不止一行,则将您的文本设置为单行,这样它就不会出现在第二行并更改每行的高度。

回答by Toorop

First of all you should use spto specify the size of a text not dip (dp), second i think you have some contradictory dimensions you have 2 text fields of 15 + 14 dp in a layout of ONLY 10 dp. Try to increase your layout height to 40 dp and change your textview's dimensions to use sp

首先,您应该使用sp来指定文本的大小而不是倾斜 (dp),其次我认为您有一些相互矛盾的尺寸,您在只有 10 dp 的布局中有 2 个 15 + 14 dp 的文本字段。尝试将布局高度增加到 40 dp 并更改 textview 的尺寸以使用 sp