状态栏颜色不随相对布局作为根元素而改变

14

我刚开始学习安卓开发,学习如何添加AppBar。但如果我将RelativeLayout作为根布局,状态栏将不使用主题中定义的“colorPrimaryDark”颜色。但将布局更改为CoordinatorLayout可以解决这个问题。

有人能解释一下两者之间的区别吗?是否需要使用CoordinatorLayout布局?

Activity扩展了AppCompatActivity。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.ankit.designdemo.MainActivity">


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

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

输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.ankit.designdemo.MainActivity">


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

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

在此输入图片描述

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</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" />


public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_activity);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        actionBar.setDisplayHomeAsUpEnabled(true);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


}

你能展示一下你定义 colorPrimaryDark 的样式吗? - hrskrs
@hrskrs 已添加到问题中。。 - Ankit
你确定你的样式页面在values-v21上吗? - hrskrs
首先,您不需要使用CoordinatorLayout来使用Toolbar。状态栏应该默认使用您的colorPrimaryDark颜色进行着色,因为您继承了AppCompatActivity,所以我无法重现您的问题。请问您在代码中做了什么操作吗? - Alex.F
@Alex.F,我刚刚创建了一个新的Android项目,使用了滚动活动(Scrolling Activity),除了main_activity.xml文件之外,我还没有触及任何代码。它自动创建了坐标布局,并且运行良好。由于我在参考一些教程,所以使用了相对布局。就这样,没有其他变化。 - Ankit
显示剩余2条评论
3个回答

32

v21/styles.xml 文件中,您可能有以下代码行:

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

由于您正在使用 API 23,所以声明的样式将被从 v21/styles.xml 中的样式覆盖。

解决方案:

只需删除那行代码就可以了,这样它就能正常工作了。


非常准确,我的问题已经解决了。由于我正在学习,请您能否添加有关覆盖的说明。我曾认为v21/styles.xml仅适用于API 23,但事实并非如此。另外,为什么使用CoordinatorLayout时一切都正常也是一个很好的补充。 - Ankit
1
@Ankit 关于样式,您可以在此处阅读有关它们如何基于平台工作的信息(https://developer.android.com/guide/topics/ui/themes.html)。关于“CoordinatorLayout”:它之所以有效,是因为有“android:fitsSystemWindows =“true””,如果您将其删除,则会显示相同的行为。 - hrskrs
1
我现在明白了,所以v21/styles.xml将在API 21或更高版本上运行。谢谢你的帮助。 - Ankit
@Ankit,是的,已声明的样式将从v21/styles.xml中获取,未声明但在styles.xml中声明的样式,将从styles.xml中获取。 - hrskrs

2

0

只需添加这个

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接