2

我有三个字符串数据数组,我想通过适配器将它们膨胀到列表中。问题是行的高度是固定的,即使我放了 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;
    }
}

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

4

2 回答 2

3

尝试在您的 getView() 中使用myInflater.inflate(R.layout.list_statistics_adapter, parent, false)而不是。myInflater.inflate(R.layout.list_statistics_adapter, null)指定 parent 时,RelativeLayout 效果更好。

于 2012-05-29T08:15:07.903 回答
3

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

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

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

于 2012-05-29T07:41:46.463 回答