0

我有个问题。我想将我的列表视图的背景颜色设置为白色,但只有行的背景颜色会发生变化。如何让背景填满整个屏幕?这是我的代码:

public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;

public MyAdapter(Context context, String[] values) {
    super(context, R.layout.list_view, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.list_view, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/chalkboardse.ttc");
    textView.setTypeface(tf);
    textView.setTextColor(Color.BLACK);
    textView.setText(values[position]);

    LinearLayout layout = (LinearLayout)rowView.findViewById(R.id.background);
    layout.setBackgroundColor(Color.WHITE);

    return rowView;
}
}

这是我的列表视图的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/background" >

<TextView 
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="20sp" >
</TextView>
</LinearLayout>

这是相应的活动:

public class AreaChooser extends ListActivity {

static final String [] FRUIT = new String [] { "Apple", "Pear"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setListAdapter(new MyAdapter(this, FRUIT));

}
}
4

2 回答 2

1

您正在设置列表视图行的背景,而不是列表视图本身的背景。

在你的ListActivity简单做:

getListView().setBackgroundColor(Color.WHITE);
于 2012-10-01T19:41:53.383 回答
0

LinearLayout需要在R.layout.list_viewxml 中设置为 fill_parent 或 match_parent。

于 2012-10-01T19:39:38.907 回答