gpt4 book ai didi

android - 当半透明状态设置为 true 时,状态栏颜色保持无颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:09:35 24 4
gpt4 key购买 nike

首先,我想提一下,有大量的问题和不同质量的答案,实际上我找不到很好的解释、推理和解决问题的方法。

我要:

  1. windowTranslucentStatus=true 的主题
  2. 有时会在状态栏下“绘制”(例如 DrawerLayout)
  3. 有时在状态栏下“不绘制”,让“组件/系统”绘制 colorPrimaryDark 而不是我。 (可能还有 android:fitsSystemWindows="true")

从各种资源中,我意识到根布局之间存在差异,因此我的所有试验都是使用似乎最适合并为此做好准备的 CoordinatorLayout 完成的。 (直接来自支持库,包含在所有带有工具栏、应用栏布局等的示例中)

我当前的设置:

编译 api 27,支持 libs 是版本 27*

activity_main.xml:

<?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=".MainActivity">

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

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

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

<fragment
android:id="@+id/fragment"
android:name="com.sygic.travel.materialtest5.MainActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

values/styles.xml:

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
</resources>

values-v21/styles.xml:

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>
</resources>

结果是:

toolbar with status bar without color


问题:

  1. 是否真的可以使用 windowTranslucentStatusfitsSystemWindows 而没有“白色/灰色”状态栏?
  2. fitsSystemWindows 时,CoordinationLayout 似乎支持绘制状态栏颜色,但(内部)属性(用于处理此问题)仍然为 null,如何解决此问题?还是仅用于手动设置状态栏的颜色?
  3. 在哪种情况下会考虑样式选项 android:statusBarColor?哪个组件使用这个值?为什么 CoordinationLayout 不选择此配置(使用其默认的 primaryColorDark)并使用它?
  4. 是否以某种方式支持任何其他布局? (相对、线性、约束)当它们具有这些(所述)缺陷时,它们是否真的被推荐为根布局?

注意事项:

最佳答案

好的,我尝试检查代码并发现这是解决方案! :是的:

首先,回答我的问题:

  1. 是的,是的。应用程序( View )有责任绘制一些东西。 DrawerLayout 自动完成此操作,CoordinatorLayout 也可以,请看下一个。
  2. 有一个属性 app:statusBarBackground 可以直接在样式中的 xml 标记上设置!
  3. 抱歉,我不知道(还不知道!:))
  4. 通过快速代码走查,似乎不支持绘制状态栏。

所以解决方案是使用 CoordinatorLayoutstatusBarBackground 属性:

values-v21/styles.xml

<resources>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="statusBarBackground">@color/colorPrimary</item>
</resources>

关于android - 当半透明状态设置为 true 时,状态栏颜色保持无颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49540179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com