覆盖include的background属性以更改背景颜色

ant*_*009 8 android android-layout

Android Studio 1.5
Run Code Online (Sandbox Code Playgroud)

我调用chat_profile_header了这个布局,将在许多布局中使用.我已经设定background为靛蓝色.问题是当我在其他布局中包含此标题时,我希望能够根据该布局的主题更改背景颜色.标题中的所有内容都将保持不变.

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="@color/profile_header_indigo_500">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/profile_image"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/photorace"
        android:layout_centerVertical="true"/>

    <TextView
        android:id="@+id/tvProfileName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/profile_image"
        android:layout_toEndOf="@id/profile_image"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:fontFamily="sans-serif"
        android:textColor="@android:color/white"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这里我使用的是此布局中包含的上述标题.但是,基于这个主题,我想将标题更改为另一种background颜色灰色.但是,我认为我不能覆盖背景颜色属性.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <include
        layout="@layout/chat_profile_header"
        android:background="@color/child_header_lighter_grey"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvParentList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点,而无需根据背景创建不同的页眉布局.似乎有很多重复.

Moh*_*ami 12

这似乎不可能.您可以从java设置背景.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <include
        android:id="@+id/layout1"
        layout="@layout/chat_profile_header"/>

    <include
        android:id="@+id/layout2"
        layout="@layout/chat_profile_header"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvParentList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这个java代码

Linealayout layout1 = (Linealayout)findViewbyId(R.id.layout1);
Linealayout layout2 = (Linealayout)findViewbyId(R.id.layout2);

layout1.setBackgroundColor(R.color.color_1);
layout2.setBackgroundColor(R.color.color_2);
Run Code Online (Sandbox Code Playgroud)