android - 分别处理不同 Activity 上的工具栏外观

标签 android android-toolbar

我有 2 个 Activity ,例如 Activity1 和 Activity2

并且在这两个 Activity 中我添加了工具栏

代码

Activity 1:

    <android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:id="@+id/toolbarTrendingActivity"
    android:elevation="5dp"
    android:textAlignment="center"
    android:background="@color/colorPrimary"
    app:theme="@style/ToolBarStyle">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textAlignment="center"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title"
        android:textSize="20sp"
        android:textStyle="bold"
        android:background="#00000000"
        android:textColor="#ffffff"
        android:text="Title" />

</android.support.v7.widget.Toolbar>

Activity 2

    <android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:id="@+id/toolbarTagDetailActivity"
    android:elevation="5dp"
    android:textAlignment="center"
    android:background="@color/colorPrimary"
    app:theme="@style/ToolBarStyle">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textAlignment="center"
        android:text="toolbar2"
        android:layout_gravity="center"
        android:id="@+id/textView_ToolbarTagDetailActivity_title"
        android:textSize="20sp"
        android:textStyle="bold"
        android:background="#00000000"
        android:textColor="#ffffff" />

</android.support.v7.widget.Toolbar>

如您所见,两个工具栏都有一个唯一的 ID

我正在使activity2的工具栏透明,工作正常,但是当我从activity2切换到activity1时,我的activity1的 工具栏也是透明的

为了取回它,我在 Activity1 上执行此操作:

@Override
protected void onCreate() {
  toolbarBackground = toolbar.getBackground(); 
}


    @Override
protected void onResume() {
    super.onResume();
    int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        toolbar.setBackgroundDrawable(toolbarBackground);
    } else {
        toolbar.setBackground(toolbarBackground);
    }

}

但是上面的代码没有做任何事情我也尝试像这样手动设置背景颜色 enter image description here

但它没有返回正确的颜色,我的primaryColor是蓝色,但现在工具栏的背景是紫色

任何人都可以指导我如何解决这个问题?

编辑

工具栏样式:

    <style name="ToolBarStyle"
    parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="android:textColorSecondary">@android:color/white</item>
    <item name="actionMenuTextColor">@color/colorAccent</item>
    <item name="android:background">@color/colorPrimary</item>
    <item name="android:windowActionBarOverlay">true</item>
</style>

默认工具栏(所需工具栏)

enter image description here

当前工具栏(从 Activity2 切换回来后)

enter image description here

最佳答案

你不需要使用这个,

toolbarBackground = toolbar.getBackground(); 

并且无需在 onResume() 中执行任何操作。

只需使用设置

toolbar.setBackgroundResource(R.color.price_color);

在您的 Activity 中 onCreate() 中的此代码块之后

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

如果您在

中遇到错误

toolbar.setBackgroundColor(R.color.price_color);

使用ContextCompat.getColor方法来实现

toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.primary_color));

关于android - 分别处理不同 Activity 上的工具栏外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38572096/

相关文章:

android - SharedPreferences 的工作原理及其安全性

java - setFlags 后工具栏与状态栏重叠

操作栏上的 Android 主页按钮不显示

android - 如何在工具栏 Android 中使用 SearchView

android - 按下主屏幕按钮关闭时,相机应用程序崩溃

java - android tcp客户端文件接收

android - 我可以从工具栏中删除汉堡包(抽屉导航图标),删除它的区域(矩形)吗?

android - 半透明状态栏和工具栏

android - 无法解决Android Studio中的依赖关系

android - 显示 android 软键盘时 ScrollView 向上推