如何在Listview中添加子项和空间

时间:2015-02-07 11:32:43

标签: android list listview android-listview

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

enter image description here

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

我的list.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;
}
}

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

这是您自定义行的最平等(因此最快)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)

答案 2 :(得分:0)

<?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