How to reduce listview item height

2019-08-22 03:23发布

I have an expandable listview. The rows consist of a textview and a custom checkbox. The size of the checkbox depends on the textview's text size. There are only two available sizes. When the large checkbox is shown, the difference between the rows is small. When the small checkbox is shown, it looks big. How can I reduce the row height between the rows?

<?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="wrap_content"
 android:id="@+id/childlayout"
 android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_centerVertical="true"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp" >

            <TextView
                android:id="@+id/child_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:gravity="center_vertical"
                android:textColor="#000000"/>

        </LinearLayout>

        <CheckBox
            android:id="@+id/multiple_checkbox"
            android:layout_width="wrap_content"
            android:layout_centerVertical="true"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:checked="true"
            android:layout_marginRight="10dp"
            android:button="@drawable/productlists_custom_cb"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false" />
    </RelativeLayout>
</LinearLayout>

If I set the layout_height of the relative layout to 30dp, the row height will be smaller. But this shouldn't be the way.

This is the .xml for setting the smaller checkbox on and off:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/cbon348" />
<item android:state_checked="false" android:drawable="@drawable/cboff348" />
</selector>

One could think the canvas for the image is much bigger than the image itself, but I assure you, it isn't.

enter image description here

EDIT:

I simplified the code to as below, and still nothing:

<?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="wrap_content"
 android:id="@+id/childlayout"
 android:orientation="horizontal">

            <TextView
                android:id="@+id/child_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                    android:layout_marginLeft="50dp" 
                android:gravity="center_vertical"
                android:textColor="#000000"/>

        <CheckBox
            android:id="@+id/multiple_checkbox"
            android:layout_width="wrap_content"
                 android:layout_gravity="center_vertical"
                  android:gravity="center_vertical"
            android:layout_height="wrap_content"
            android:checked="true"
            android:layout_marginRight="10dp"
            android:button="@drawable/productlists_custom_cb"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false" />

</LinearLayout>

标签: android
2条回答
家丑人穷心不美
2楼-- · 2019-08-22 03:43

My solution was to create the checkbox images in different sizes then I set the appropriate image according to the screen size:

     if ((getResources().getConfiguration().screenLayout &  Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE)
     {
        holder.checkBox.setButtonDrawable(R.drawable.productlists_custom_cb48);
     }
     else if ((getResources().getConfiguration().screenLayout &  Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL)
     {
         holder.checkBox.setButtonDrawable(R.drawable.productlists_custom_cb48);
     }
     else if ((getResources().getConfiguration().screenLayout &  Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL)
     {
         holder.checkBox.setButtonDrawable(R.drawable.productlists_custom_cb36);
     }
     holder.checkBox.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, GetDipsFromPixel(20)));
查看更多
Juvenile、少年°
3楼-- · 2019-08-22 03:51

Try using negative padding on the textview to reduce that space.

android:paddingTop="-5dp"
android:paddingBottom="-5dp"
查看更多
登录 后发表回答