android listview item height
Asked Answered
L

7

54

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?

Locoweed answered 22/11, 2010 at 9:19 Comment(1)
try this #3361923Hays
T
136
  android:textAppearance="?android:attr/textAppearanceLarge" 

seemed no effect.

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

changed the height for me

Twila answered 25/11, 2010 at 7:10 Comment(2)
@Bamboo, how to set this in the program?Wilt
add android:minHeight as attribute to the textview element or set it programmaticly from java codesTwila
A
37

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.

Abigael answered 9/9, 2011 at 23:19 Comment(1)
Spent almost an hour trying to find a solution. This is exactly what did the trick for me.Cristiecristin
E
11

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" />
Enthymeme answered 12/5, 2012 at 22:29 Comment(0)
S
5

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.

Salverform answered 15/4, 2013 at 8:57 Comment(0)
W
1

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".

Winkle answered 10/12, 2012 at 10:5 Comment(0)
A
0

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;

Autoharp answered 19/7, 2016 at 16:8 Comment(0)
L
-2

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>
Lettered answered 18/2, 2013 at 16:39 Comment(1)
Unnecessary nesting is bad practice and bad for performance. The nested LinearLayout doesn't make a different here, either.Enthymeme

© 2022 - 2024 — McMap. All rights reserved.