android 列表视图项目高度

发布于 2024-10-03 21:12:07 字数 840 浏览 0 评论 0 原文

为什么当我将 SimpleCursorAdapter 用于 ListView 时,我在 ListView 中的项目高度如下 - listview image

(我的代码基于 这个)

但是使用数组时Listview项目的高度很大

< img src="https://i.sstatic.net/tSrYu.png" alt="listview big">

(我基于 这个

项目列表视图的行布局是

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

所以我的问题是为什么使用ArrayAdapter和SimpleCursorAdapter时行高存在差异?

Why when i use SimpleCursorAdapter for ListView i have items height in ListView like this - listview image

(My code based on this)

But when using arrays Listview items have big height

listview big

(I learn listview based on this)

Row layout for item listview is

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

So My question is why there is a difference in row heights when using ArrayAdapter and SimpleCursorAdapter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

九命猫 2024-10-10 21:12:07
  android:textAppearance="?android:attr/textAppearanceLarge" 

似乎没有效果。

  android:minHeight="?android:attr/listPreferredItemHeight" 

为我改变了高度

  android:textAppearance="?android:attr/textAppearanceLarge" 

seemed no effect.

  android:minHeight="?android:attr/listPreferredItemHeight" 

changed the height for me

迷路的信 2024-10-10 21:12:07

对我来说,诀窍不是设置高度,而是设置 minHeight。这必须应用于自定义适配器用于呈现每一行的任何布局的根视图。

The trick for me was not setting the height -- but instead setting the minHeight. This must be applied to the root view of whatever layout your custom adapter is using to render each row.

愛上了 2024-10-10 21:12:07

您需要在列表项布局上使用填充,以便在项目边缘添加空间(仅增加字体大小并不能做到这一点)。

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:padding="8dp" />

You need to use padding on the list item layout so space is added on the edges of the item (just increasing the font size won't do that).

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:padding="8dp" />
三生池水覆流年 2024-10-10 21:12:07

我做了类似的事情:

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

    View view = super.getView(position, convertView, parent);

    TextView textView = (TextView) view.findViewById(android.R.id.text1);
    textView.setHeight(30);
    textView.setMinimumHeight(30);

    /*YOUR CHOICE OF COLOR*/
    textView.setTextColor(Color.BLACK);

    return view;
}

您必须将两个字段都放入 textView.setHeight(30);
textView.setMinimumHeight(30);否则它不会改变任何事情。对我来说它有效&我有同样的问题。

I did something like that :

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

    View view = super.getView(position, convertView, parent);

    TextView textView = (TextView) view.findViewById(android.R.id.text1);
    textView.setHeight(30);
    textView.setMinimumHeight(30);

    /*YOUR CHOICE OF COLOR*/
    textView.setTextColor(Color.BLACK);

    return view;
}

You must put the both fields textView.setHeight(30);
textView.setMinimumHeight(30); or it won't change anything. For me it worked & i had the same problem.

黯淡〆 2024-10-10 21:12:07

列表视图项目的高度根据其内容进行调整。第一张图片中没有内容。所以高度非常低。在第二张图片中,高度根据文本的大小而增加。因为,您指定了 android:layout_height="wrap_content"。

The height of list view items are adjusted based on its contents. In first image, no content. so height is very minimum. In second image, height is increased based on the size of the text. Because, you specified android:layout_height="wrap_content".

策马西风 2024-10-10 21:12:07

这是我的解决方案;
这是我在 BaseAdapter 子类中的 getView() :

public View getView(int position, View convertView, ViewGroup parent)
{
    if(convertView==null)
    {
        convertView=inflater.inflate(R.layout.list_view, parent,false);
        System.out.println("In");
    }
    convertView.setMinimumHeight(100);
    return convertView;
}

这里我将 ListItem 的最小高度设置为 100;

Here is my solutions;
It is my getView() in BaseAdapter subclass:

public View getView(int position, View convertView, ViewGroup parent)
{
    if(convertView==null)
    {
        convertView=inflater.inflate(R.layout.list_view, parent,false);
        System.out.println("In");
    }
    convertView.setMinimumHeight(100);
    return convertView;
}

Here i have set ListItem's minimum height to 100;

梦幻的味道 2024-10-10 21:12:07

这是我的解决方案(有一个嵌套的 LinearLayout):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/item"
            android:layout_width="match_parent"
            android:layout_height="47dp"
            android:background="@drawable/box_arrow_top_bg"
            android:gravity="center"
            android:text="全部收支"
            android:textColor="#666"
            android:textSize="16sp" />
    </LinearLayout>

</LinearLayout>

This is my solution(There is a nested LinearLayout):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/item"
            android:layout_width="match_parent"
            android:layout_height="47dp"
            android:background="@drawable/box_arrow_top_bg"
            android:gravity="center"
            android:text="全部收支"
            android:textColor="#666"
            android:textSize="16sp" />
    </LinearLayout>

</LinearLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文