【问题标题】:How to tune width of listview, header and footer correctly?如何正确调整列表视图、页眉和页脚的宽度?
【发布时间】:2014-04-25 18:30:27
【问题描述】:

我正在使用 ListView,它有页眉和页脚。问题是我需要项目列表本身比标题更窄。而我无法做到这一点。问题是,如果我为 ListView 设置填充或边距,它将被设置为列出项目、页眉和页脚(由于某种原因,页眉和页脚会比列表项目更窄)。仅为项目布局本身设置边距是没有用的。以下是布局:

main_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

header_list_layout.xml:

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

    <EditText
        android:inputType="textPersonName" />

</RelativeLayout>

footer_list_layout.xml:

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

    <TextView
        android:layout_width ="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

list_item_layout.xml:

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

    <TextView
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </TextView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </TextView>

</LinearLayout>

那么我该怎么做才能使一切正常呢?

我希望它看起来如何的图像:

【问题讨论】:

    标签: android listview android-listview margins


    【解决方案1】:

    您可以在adapter 中为ListView 中的项目设置自定义布局。例如,如果你使用ArrayAdapter,你可以这样做(注意getView方法):

    public class MySimpleArrayAdapter extends ArrayAdapter<String> {
      private final Context context;
      private final String[] values;
    
      public MySimpleArrayAdapter(Context context, String[] values) {
        super(context, R.layout.rowlayout, values);
        this.context = context;
        this.values = values;
      }
    
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_item, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        textView.setText(values[position]);
        // change the icon for Windows and iPhone
        String s = values[position];
        if (s.startsWith("iPhone")) {
          imageView.setImageResource(R.drawable.no);
        } else {
          imageView.setImageResource(R.drawable.ok);
        }
    
        return rowView;
      }
    } 
    

    (复制自this tutorial的代码)

    然后在您的list_item.xml 中,将填充添加到LinearLayout,您将看到更窄的视图:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="#0000ff"
                  android:paddingLeft="10dp"
                  android:paddingRight="10dp"
        >
            <TextView
                android:id="@+id/tv1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#AAAA00"
                />
            <TextView
                android:id="@+id/tv2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00aAAA"
                />
    </LinearLayout>
    

    您将获得:


    或者您可以将边距应用于个人TextView

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="#0000ff"
        >
            <TextView
                android:id="@+id/tv1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#AAAA00"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                />
            <TextView
                android:id="@+id/tv2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00aAAA"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                />
    </LinearLayout>
    

    你会得到:


    或者如果布局中有很多子视图,可以用另一个 LinearLayout 包裹它(尽管它可能会影响性能)并在其上应用边距

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="#0000ff"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#AAAAAA"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            >
    
            <TextView
                android:id="@+id/tv1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#AAAA00"
                />
            <TextView
                android:id="@+id/tv2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00aAAA"
                />
            </LinearLayout>
    </LinearLayout>
    

    您将获得:


    【讨论】:

    • 是的,我正在为列表项使用自定义布局。正如我在主帖中所说,在此自定义布局中设置边距或填充并不能解决问题。
    • 我的适配器没有问题。请发布您的布局文件和适配器代码,以便我们为您提供帮助。
    • 可能是我没有说清楚:我的问题是列表项(页眉和页脚之间的部分)比页眉宽。我需要相反的东西。而且我的布局没有具体信息。但是好的,我会更新问题。
    • 所以你想让列表项比标题更窄。我最初的建议是为列表项自定义布局,以便您可以更改其外观(例如使其更窄)。我的回答已更新为包含带有填充的布局。
    • 添加填充只是让列表项更宽。 :) 并且添加 layout_margin 没有任何效果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多