2

これは私の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"));

これを正しく理解していれば、リストビューの親 (この場合は LinearLayout) の背景色が変更されるはずです。しかし、これは機能していません。何が間違っていますか?

4

2 に答える 2

3

あなたは混乱していると思います.getRootView()はそのLinearLayoutを取得しませんが、そのビューの親を取得します. この他の質問を参照してください。listview.getParent()代わりに使用する必要があります。キャストする必要があることに注意してください。

于 2013-01-10T14:47:19.510 に答える
1

getParent()の代わりに使用しgetRootView()て、周囲のインスタンスを取得してみてくださいLinearLayout..

于 2013-01-10T14:47:28.117 に答える