Android:在其他活动中使用相同的工具栏背景

问题描述 投票:0回答:6

我正在使用AppCompatActivity跟随

<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>

假设背景设置为红色,它在主活动中很好,但我的其他活动在打开时恢复为默认蓝色。我错过了什么?

编辑:我不是说设置活动的背景,而只是设置工具栏。

android android-toolbar appcompatactivity
6个回答
1
投票

如果您想在一个地方为所有活动设置背景,这个答案将会有所帮助:https://stackoverflow.com/a/37799081/2318843


1
投票

您可以在应用程序样式(<item name="android:background">@drawable/background</item>)中添加此<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">,如果您想在应用程序中的每个位置背景。

确定对于工具栏,像这样创建单独的xml布局

<?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>

现在把它包含在你想要的任何地方!喜欢

<?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>

0
投票

在其他活动中也设置:android:background="@drawable/red"


0
投票

把这一行放在所有活动中:android:background =“@ drawable / red”


0
投票

如果您只是想为所有活动使用一个背景,那么只需在您的应用主题中创建一个背景标记,因为您将该主题应用于每个活动,它将自动添加到那里,您不需要再添加一行如果你想改变一个主题的背景,打开那个活动的xml并在那里添加背景标签,它会将该背景添加到该特定活动。


0
投票

需要在xml文件中进行一些更改..您必须在工具栏控件中添加以下行。

android:background="@drawable/red"

您使用工具栏控件的每个地方都是这样的。

<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"
/>

在每个xml文件中更改该控件的背景。

© www.soinside.com 2019 - 2024. All rights reserved.