如何在android中设置包含背景颜色

时间:2015-02-26 14:16:07

标签: android android-layout

如何在Android中的include标记上设置背景颜色?这不起作用:

 <include
 android:id="@+id/list_item_section_text"
 android:layout_width="fill_parent"
 android:layout_height="match_parent"
 layout="@android:layout/preference_category"
 android:background="%MY_BACKGROUND%"/>

3 个答案:

答案 0 :(得分:8)

  

您无法为include标记提供背景色。

为什么?

很明显,如果你能够将背景颜色赋予include标签,那么它将全部搞砸了你的include颜色和可能应用于layout的另一种颜色。已经包含的内容。

答案 1 :(得分:8)

如果你不是“太深视图树 - 偏执狂”类型的家伙,你可以将你的include包裹在FrameLayout 中:

<FrameLayout
    android:id="@+id/list_item_section_text"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="%YOUR_BACKGROUND%">

        <include layout="@android:layout/preference_category"/>

</FrameLayout>

编辑:当然,不要忘记先从android:background布局中删除preference_category.xml

答案 2 :(得分:0)

试试这个:

<include
    android:id="@+id/list_item_section_text"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    layout="@android:layout/preference_category"/>

在首选项类别布局中:

<LinearLayout
    android:id="@+id/preference_category"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@colors/white"/>

否则RUNTIME中的更改

preference_category.setBackgroundResource(R.id.bckResource);