如何使用自定义列表适配器添加第三个文本视图以列出视图元素

时间:2015-09-26 00:15:21

标签: android

我按照this教程了解如何向列表视图中的各个列表元素添加标题,文本视图id/txtTitle和图像视图id/imgIcon

我修改了教程代码以合并第三个文字视图id/txtTitle2,但我仍然只能看到图片视图id/imgIcon和原始文字视图id/txtTitle。此外,列表视图似乎更大或放大。这是我的代码修改的结果。

如何修复代码,以便看到没有大小失真的第三个文本视图?

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"
    tools:context=".MainActivity">


    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

listview_item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp">

    <ImageView android:id="@+id/imgIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

    <TextView android:id="@+id/txtTitle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textStyle="bold"
        android:textSize="22dp"
        android:textColor="#000000"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

    <TextView android:id="@+id/txtTitle2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textStyle="bold"
        android:textSize="22dp"
        android:textColor="#000000"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

</LinearLayout>

自定义列表适配器WeatherAdapter extends ArrayAdapter<Weather>

public class WeatherAdapter extends ArrayAdapter<Weather>{

    Context context;
    int layoutResourceId;
    Weather data[] = null;

    public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
            holder.txtTitle2 = (TextView)row.findViewById(R.id.txtTitle2);

            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }

        Weather weather = data[position];
        holder.txtTitle2.setText(weather.title2);
        holder.txtTitle.setText(weather.title);
        holder.imgIcon.setImageResource(weather.icon);

        return row;
    }

    static class WeatherHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
        TextView txtTitle2;
    }
}

班级Weather

public class Weather {
    public int icon;
    public String title;
    public String title2;

    public Weather(){
        super();
    }

    public Weather(int icon, String title, String title2) {
        super();
        this.icon = icon;
        this.title = title;
        this.title2 = title2;
    }
}

1 个答案:

答案 0 :(得分:1)

在您的行布局中,第二个和第三个文本视图的宽度都设置为fill _ parent ..因此在这种情况下,第二个TextView占据屏幕的整个宽度,第三个TextView没有剩余空间。只需将这些属性更改为wrap_content,或者使用权重代替..