如何更改android app /大屏幕的背景颜色?

时间:2012-10-01 19:37:19

标签: android listview background android-listview

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

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));

}
}

2 个答案:

答案 0 :(得分:1)

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

ListActivity只需执行:

getListView().setBackgroundColor(Color.WHITE);

答案 1 :(得分:0)

LinearLayout需要在R.layout.list_view的xml中设置为fill_parent或match_parent。