这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background"
android:orientation="vertical"
android:padding="15dip" >

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

现在在我的代码中,我想改变窗口的背景色,我这样做:
ListView listview = (ListView) findViewById(android.R.id.list);
View root = listview.getRootView();
root.setBackgroundColor(Color.parseColor("#bdbdbd"));

如果我正确理解了这一点,那么这应该会更改listview父级的背景色(在本例中是linearlayout)。但是这不起作用,我做错什么了?

最佳答案

我想你很困惑,getrootview()不会让你得到线性布局,而是那个视图的父视图。见this other question。您应该改为使用listview.getParent();请注意,它需要进行铸造。

07-28 00:36
查看更多