Android,如何在列表适配器中增加行的高度?

时间:2012-05-29 07:34:24

标签: android android-listview listadapter

我有三个String数据数组,我想通过适配器在列表中对它们进行充气。问题是行的高度是固定的,即使我把30dip,50dip,100dip增加,高度也没有生效:(

这是我的行代码的XML代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
    xmlns:android                   = "http://schemas.android.com/apk/res/android"
    android:layout_width            = "fill_parent"
    android:layout_height           = "fill_parent" // even when i change it to 30dip, 50dip and etc. doesn't take effect
    android:background              = "@color/septator_grey_light">


    <TextView 
        android:id                      = "@+id/tvStatisticsTitle"
        android:layout_width            = "wrap_content"
        android:layout_height           = "wrap_content"
        android:layout_centerInParent   = "true"
        android:gravity                 = "center_vertical|center"
        android:text                    = "@string/test_10"
        style                           = "@style/simpletopic.black" />


    <TextView
        android:id                      = "@+id/tvStatisticsHost"
        android:layout_width            = "wrap_content"
        android:layout_height           = "wrap_content"
        android:layout_alignBaseline    = "@+id/tvStatisticsTitle"
        android:layout_alignBottom      = "@+id/tvStatisticsTitle"
        android:layout_alignParentLeft  = "true"
        android:layout_marginLeft       = "20dp"
        android:text                    = "@string/test_02"
        style                           = "@style/simpletopic.black.big" />


    <TextView
        android:id                      = "@+id/tvStatisticsGeust"
        android:layout_width            = "wrap_content"
        android:layout_height           = "wrap_content"
        android:layout_alignBaseline    = "@+id/tvStatisticsTitle"
        android:layout_alignBottom      = "@+id/tvStatisticsTitle"
        android:layout_alignParentRight = "true"
        android:layout_marginRight      = "20dp"
        android:text                    = "@string/test_02"
        style                           = "@style/simpletopic.black.big" />

</RelativeLayout>

我的适配器代码如下。

public class StatisticsAdapter extends BaseAdapter {

    private final String TAG = "*** StatisticsAdapter ***";
    private final String[] TITLE = {"Shots on Target", "Shots Wide", "Offsides", "Corners", "Possession", "Fouls"};

    private LayoutInflater myInflater;

    private String[] hostInfo;
    private String[] guestInfo;

    public StatisticsAdapter(Context context) {
        myInflater = LayoutInflater.from(context);

        Log.i(TAG, "Adapter setuped successfully.");
    }

    public void setData(Statistics statisticsItems) {

        hostInfo  = new String[6];
        guestInfo = new String[6];

        hostInfo[0]  = statisticsItems.getHomeTeamShotsTarget();
        hostInfo[1]  = statisticsItems.getHomeTeamShotsWide();
        hostInfo[2]  = statisticsItems.getHomeTeamOffsides();
        hostInfo[3]  = statisticsItems.getHomeTeamCorners();
        hostInfo[4]  = statisticsItems.getHomeTeamPossession();
        hostInfo[5]  = statisticsItems.getHomeTeamFouls();

        guestInfo[0] = statisticsItems.getAwayTeamShotsTarget();
        guestInfo[1] = statisticsItems.getAwayTeamShotsWide();
        guestInfo[2] = statisticsItems.getAwayTeamOffsides();
        guestInfo[3] = statisticsItems.getAwayTeamCorners();
        guestInfo[4] = statisticsItems.getAwayTeamPossession();
        guestInfo[5] = statisticsItems.getAwayTeamFouls();

        Log.i(TAG, "Data passed to the adapter.");
    }

    @Override
    public int getCount() {
        return TITLE.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;        

        if (convertView == null) {
            convertView = myInflater.inflate(R.layout.list_statistics_adapter, null);
            holder      = new ViewHolder();
            holder.tvStatisticsHost   = (TextView) convertView.findViewById(R.id.tvStatisticsHost);
            holder.tvStatisticsTitle  = (TextView) convertView.findViewById(R.id.tvStatisticsTitle);
            holder.tvStatisticsGeust  = (TextView) convertView.findViewById(R.id.tvStatisticsGeust);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.tvStatisticsHost.setText(hostInfo[position]);
        holder.tvStatisticsTitle.setText(TITLE[position]);
        holder.tvStatisticsGeust.setText(guestInfo[position]);

        return convertView;
    }

    static class ViewHolder {
        TextView tvStatisticsHost;
        TextView tvStatisticsTitle;
        TextView tvStatisticsGeust;
    }
}

任何建议将不胜感激。感谢

2 个答案:

答案 0 :(得分:3)

高度必须是wrap_content。添加一些具有固定高度和宽度的视图(空视图)并将它们用作边距。

<View
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="20dp" />

这应该可以解决问题。您甚至可以将背景更改为您想要的任何内容。 (但是如果要显示背景,则应设置一些宽度)

答案 1 :(得分:3)

尝试在getView()中使用myInflater.inflate(R.layout.list_statistics_adapter, parent, false)代替myInflater.inflate(R.layout.list_statistics_adapter, null)。指定父级时,RelativeLayout的效果会更好。