问题描述:

I am using AppCompatActivity with following

<android.support.design.widget.AppBarLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@drawable/red"

android:fitsSystemWindows="true"

android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

app:popupTheme="@style/AppTheme.PopupOverlay"

app:navigationIcon="@mipmap/app_icon"

/>

</android.support.design.widget.AppBarLayout>

Let's say the background is set to red, it is fine in the main activity, but my other activity restore to default blue when open.

what did I miss?

Thank you

edit: I am not saying to set the background of the activity, but only the toolbar.

网友答案:

If you want to set background for all activities in one place, this answer will be helpful: http://stackoverflow.com/a/37799081/2318843

网友答案:

You can add this <item name="android:background">@drawable/background</item> in app style (<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">), if you want background every where in app.

Ok For toolbar create separate xml layout like this

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="@drawable/backgroundImage"//<--Add your background here
    android:elevation="1dp"
    app:layout_collapseMode="pin">

    <com.trptic.driver.widgets.TTextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:maxLines="1"
        android:textColor="@color/black"
        android:textSize="@dimen/_14sdp" />
</android.support.v7.widget.Toolbar>

Now include it wherever you want it! like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:font="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <include layout="@layout/toolbar" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        ....
    </FrameLayout>
</LinearLayout>
网友答案:

Do set the : android:background="@drawable/red" in other Activity as well.

网友答案:

Put this line in all activity : android:background="@drawable/red"

网友答案:

if you just want to use one background for all of your activities then just create a tag of background in your app theme and since you'll apply that theme to every activity it will automatically get added there and you wont need to add one more line and if in case you want to change background for just one theme open up xml of that activity and add background tag there it will add that background to that specific activity.

网友答案:

Again you have minimum set of data given,

But have a look what you have done!

 <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/red"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:navigationIcon="@mipmap/app_icon"
            />
   </android.support.design.widget.AppBarLayout>

Now for your android.support.design.widget.AppBarLayout you have a android:theme and also you have given a android:background you will able to see whatever it is in android:background!

For you Blue view xml i have no clue but I think you are missing android:background so it takes the color form android:theme

What you can do to keep a static color? Your view needs to be same with the color

  • One way will be keep that view(if its gonna be the same) as a separate layout and add it using include tag across all activity views you have.
  • Second way is always use the android:background="@drawable/red" for android.support.design.widget.AppBarLayout
  • Third one is set that color in your theme.So no need to set android:background again !

eg:

 <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
         .. other // items 
        <item name="colorPrimary">@color/colorAccent</item> // this will do the job change the color as you want
    </style>
网友答案:

Need to make some change in your xml file.. You have to put below line in your Toolbar control.

android:background="@drawable/red"

Every where you use Toolbar Control like this.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:navigationIcon="@mipmap/app_icon"
/>

In every xml files to change background of that control.

相关阅读:
Top