【问题标题】:How to add subitem and space in Listview如何在 Listview 中添加子项和空间
【发布时间】:2015-02-07 11:32:43
【问题描述】:

我在 android 中使用自定义列表视图。

这是我的列表视图。我需要在两个列表之间添加一些空格或增加边框大小。怎么做? 以及如何添加潜台词?

我的列表.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TableRow>
        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp"/>

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="50dp" />



</TableRow>
</TableLayout>

customlist.java

package in.wptrafficanalyzer.locationplacesautocomplete;


import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomList extends ArrayAdapter<String>{

private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;

}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);

ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);

imageView.setImageResource(imageId[position]);
return rowView;
}
}

【问题讨论】:

  • 在您的自定义行中添加一些边距或内边距。顺便说一句,该行不需要 ViewGroup 作为容器:它可以是带有复合可绘制对象的单个 TextView。不需要更多了。尽可能保持布局平坦,
  • 如何添加?对不起。我是安卓新手

标签: android list listview android-listview


【解决方案1】:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <ImageView
        android:id="@+id/img"
         android:layout_width="50dp"
            android:layout_height="50dp"/>

      />
    <LinearLayout
       android:background="#E5E4E2"
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/txt"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:gravity="center_vertical"
        />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" 
            android:id="@+id/sub"
            android:singleLine="true"
            android:ellipsize="marquee"
        />

    </LinearLayout>
</LinearLayout>

【讨论】:

    【解决方案2】:

    这是您的自定义行的 flattest(因此也是 最快)xml 布局设计(不需要 ViewGroup):

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

    看,我添加了 8dp 的布局边距。

    在你的代码中,那么:

    @Override
    public View getView(int position, View view, ViewGroup parent)
    {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_single, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
    
        txtTitle.setText(web[position]);
        // This is where you set the image to the left of the TextView:
        txtTitle.setCompoundDrawablesWithIntrinsicBounds (imageId[position], nul, null, null);
        return rowView;
    }
    

    关于 setCompoundDrawablesWithIntrinsicBounds 的参考: http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)

    【讨论】:

      【解决方案3】:
      <?xml version="1.0" encoding="utf-8"?>
      <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >
      
      <TableRow 
      android:layout_margin="5dp" >
      //USE MARGIN TO INDENT THE TABLEROW
          <ImageView
              android:id="@+id/img"
              android:layout_width="50dp"
              android:layout_height="50dp"/>
      
          <TextView
              android:id="@+id/txt"
              android:layout_width="wrap_content"
              android:layout_height="50dp" />
      </TableRow>
      </TableLayout>
      //YOU CAN ADD WHATEVER SUB TEXT IN TABLEROW IN ADDITION TO txt as long as you add it to String[] and Integer [] in Java
      

      【讨论】: