【问题标题】:Android status bar is the same color as ToolbarAndroid状态栏与Toolbar颜色相同
【发布时间】:2022-04-17 14:07:19
【问题描述】:

我正在运行一个安卓项目。我的 activity_main 布局中的工具栏和状态栏都是绿色的。如果我将工具栏颜色更改为其他颜色,状态栏也会更改为新颜色。

我有一个带有 activity_hotel_detail.xml 布局的活动。状态栏和工具栏颜色是我需要的不同颜色。

为什么同样的Toolbar配置会导致状态栏颜色不一样?

如何使我的 main_activity 布局与我的 activity_hotel_detail 布局相同?

我是android新手,任何帮助谢谢!

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ViewController.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/red"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        style="@style/ThemeOverlay.AppCompat.Dark"/>

</RelativeLayout>

activity_hotel_detail.xml

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/Blue"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        style="@style/ThemeOverlay.AppCompat.Light"/>

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:fresco="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:fillViewport="true"
        android:background="@color/cell">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/white"
                android:padding="0dp"
                android:stretchColumns="1">
                    <com.facebook.drawee.view.SimpleDraweeView
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:contentDescription="@string/img_description"
                        fresco:placeholderImage="@drawable/test_image"
                        fresco:actualImageScaleType="fitStart"
                        android:id="@+id/hotel_image_view" />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="New Text"
                        android:id="@+id/hotel_name"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="200dp"
                        android:text="New Text New Text New Text New TextNew TextNew TextNew TextNew TextNew TextNew TextNew TextNew TextNew TextNew Text"
                        android:id="@+id/hotel_description"/>
                <fragment
                    class="com.example.william.willhotel.ViewController.HotelDetailFragment"
                    android:layout_width="match_parent"
                    android:layout_height="30dp"
                    android:id="@+id/fragement">
                    </fragment>
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="back"
                    android:background="@color/BlueViolet"
                    android:id="@+id/back_button" />
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="next"
                    android:background="@color/DarkRed"
                    android:id="@+id/next_button" />

            </LinearLayout>
    </ScrollView>

编辑:styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/DarkRed</item>
        <item name="colorPrimaryDark">@color/IndianRed</item>
        <item name="colorAccent">@color/yellow</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

styles.xml(v21)

<resources>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>

EDIT2 在 styles.xml(v21) 中将 'transparent1 替换为 @color/IndianRed 无效。

<resources>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@color/IndianRed</item>
    </style>
</resources>

编辑

我的activity扩展自AppCompatActivity,我看到源码只有大于23的sdk版本才能获取系统主题。

public class MainActivity extends AppCompatActivity

所以如果我在 sdk 23 下运行,状态栏会按预期显示正确。

if (delegate.applyDayNight() && mThemeId != 0) {
            // If DayNight has been applied, we need to re-apply the theme for
            // the changes to take effect. On API 23+, we should bypass
            // setTheme(), which will no-op if the theme ID is identical to the
            // current theme ID.
            if (Build.VERSION.SDK_INT >= 23) {
                onApplyThemeResource(getTheme(), mThemeId, false);
            } else {
                setTheme(mThemeId);
            }
        }

【问题讨论】:

标签: android android-layout


【解决方案1】:

在您的活动的 onCreate() 方法中使用以下代码

  if (Build.VERSION.SDK_INT >= 21) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.primary_dark));
        }

primary_dark color 是工具栏的颜色

【讨论】:

  • 您是否使用以下样式-v21 true@android:color/transparenttrue
  • 其实我也不是很清楚。但我粘贴了两个文件:styles.xml 和 styles.xml(v21)
  • 是的。刚刚检查过。
  • 在styles.xml(v21)中也添加这个true
  • 在您的 styles.xml 中将父级更改为此“Theme.AppCompat.Light.NoActionBar”
【解决方案2】:

将此添加到您的values-v21/styles.xml

<item name="android:statusBarColor">@color/IndianRed</item>

您的主题应该如下所示删除Transparent

   <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@color/IndianRed</item>
    </style>

【讨论】:

    【解决方案3】:

    这可能会对您有所帮助

     <resources>
     <!-- inherit from the material theme -->
     <style name="AppTheme" parent="android:Theme.Material">
    <!-- Main theme colors -->
    <!--   your app branding color for the app bar -->
    <item name="android:colorPrimary">@color/primary</item>
    <!--   darker variant for the status bar and contextual app bars -->
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <!--   theme UI controls like checkboxes and text fields -->
    <item name="android:colorAccent">@color/accent</item>
    

    参考:https://developer.android.com/training/material/theme.html#StatusBar

    注意:根据谷歌文档“工具栏使用 500 版本的 indigo,而状态栏使用 700 版本。”文档链接:https://www.google.com/design/spec/style/color.html#color-color-schemes

    【讨论】:

      【解决方案4】:
          <item name="android:windowBackground">@color/cardview_dark_background</item>
          <item name="android:statusBarColor">@color/cardview_dark_background</item>
      

      如果你有多个主题,它对我有用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-20
        • 2017-03-26
        • 1970-01-01
        • 2023-03-12
        相关资源
        最近更新 更多