如何降低ListView项高度 [英] How to reduce listview item height

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

问题描述

我有一个可扩展的列表视图。该行由一个TextView和一个自定义复选框。复选框的大小取决于TextView中的文字大小。只有两个可用的尺寸。当示出了大的复选框,各行之间的差异较小。当显示小复选框,它看起来很大。我怎样才能减少行之间的行高?

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
 的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:layout_width =FILL_PARENT
 机器人:layout_height =WRAP_CONTENT
 机器人:ID =@ + ID / childlayout
 机器人:方向=垂直>    < RelativeLayout的
        机器人:ID =@ + ID / relativeLayout1
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT>        <的LinearLayout
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_centerVertical =真
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginLeft =50dp>            <的TextView
                机器人:ID =@ + ID / child_title
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_gravity =center_vertical
                机器人:重力=center_vertical
                机器人:文字颜色=#000000/>        < / LinearLayout中>        <复选框
            机器人:ID =@ + ID / multiple_checkbox
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_centerVertical =真
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentRight =真
            安卓:检查=真
            机器人:layout_marginRight =10dp
            机器人:按钮=@绘制/ productlists_custom_cb
            机器人:可点击=假
            机器人:可聚焦=假
            机器人:focusableInTouchMode =FALSE/>
    < / RelativeLayout的>
< / LinearLayout中>

如果我设置相对布局30dp的layout_height,行的高度将较小。但是,这不应该的方式。

这是用于设置和关闭小复选框的.xml:

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<项目的android:state_checked =真正的机器人:可绘制=@绘制/ cbon348/>
<项目的android:state_checked =false的机器人:可绘制=@绘制/ cboff348/>
< /选择>

有人可能认为对于像画布比图像本身大很多,但我向你保证,事实并非如此。

编辑:

我简化了code到下面,并且仍然没有什么:

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
 的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:layout_width =FILL_PARENT
 机器人:layout_height =WRAP_CONTENT
 机器人:ID =@ + ID / childlayout
 机器人:方向=横向>            <的TextView
                机器人:ID =@ + ID / child_title
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_gravity =center_vertical
                    机器人:layout_marginLeft =50dp
                机器人:重力=center_vertical
                机器人:文字颜色=#000000/>        <复选框
            机器人:ID =@ + ID / multiple_checkbox
            机器人:layout_width =WRAP_CONTENT
                 机器人:layout_gravity =center_vertical
                  机器人:重力=center_vertical
            机器人:layout_height =WRAP_CONTENT
            安卓:检查=真
            机器人:layout_marginRight =10dp
            机器人:按钮=@绘制/ productlists_custom_cb
            机器人:可点击=假
            机器人:可聚焦=假
            机器人:focusableInTouchMode =FALSE/>< / LinearLayout中>


解决方案

我的解决方案是创建在不同尺寸的复选框,然后图像我根据屏幕大小设置相应的图片:

  IF((getResources()getConfiguration()屏幕布置和放大器; Configuration.SCREENLAYOUT_SIZE_MASK)== Configuration.SCREENLAYOUT_SIZE_LARGE)
     {
        holder.checkBox.setButtonDrawable(R.drawable.productlists_custom_cb48);
     }
     否则如果((getResources()getConfiguration()屏幕布置和放大器; Configuration.SCREENLAYOUT_SIZE_MASK)== Configuration.SCREENLAYOUT_SIZE_NORMAL)
     {
         holder.checkBox.setButtonDrawable(R.drawable.productlists_custom_cb48);
     }
     否则如果((getResources()getConfiguration()屏幕布置和放大器; Configuration.SCREENLAYOUT_SIZE_MASK)== Configuration.SCREENLAYOUT_SIZE_SMALL)
     {
         holder.checkBox.setButtonDrawable(R.drawable.productlists_custom_cb36);
     }
     holder.checkBox.setLayoutParams(新android.widget.RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,GetDipsFromPixel(20)));

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.

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>

解决方案

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

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

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