利用 Android 4.4 KitKat 中的半透明状态栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20578780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:05:01  来源:igfitidea点击:

Taking advantage of the translucent status bar in Android 4.4 KitKat

androiduser-interfacestatusbarandroid-4.4-kitkat

提问by Amonstan

When I released my note taking application for Android 4.0 - 4.3 I used a custom action bar color with a custom action bar icon (instead of using the standard light and dark action bars). I would like to make it so on Android 4.4, the status bar will also take on the custom color I am using in my Action Bar (which is #FFD060). Is there a way to easily change my current styles.xml to allow 4.4 to take advantage of this?

当我发布适用于 Android 4.0 - 4.3 的笔记应用程序时,我使用了带有自定义操作栏图标的自定义操作栏颜色(而不是使用标准的明暗操作栏)。我想在 Android 4.4 上做到这一点,状态栏也将采用我在操作栏中使用的自定义颜色(即 #FFD060)。有没有办法轻松更改我当前的styles.xml 以允许4.4 利用此功能?

My styles.xml under my values-v19 folder is as follows:

我的 values-v19 文件夹下的styles.xml如下:

<resources>

<style name="AppBaseTheme" parent="@android:style/Theme.Holo.Light">
   <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
</style>

 <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
     <item name="android:background">#ffd060</item>
     <item name="android:textColor">#fff</item>   
     <item name="android:icon">@drawable/action</item>
     <item name="android:titleTextStyle">@style/MyTextAppearance</item>
 </style>

 <style name="MyTextAppearance" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
     <item name="android:icon">@drawable/action</item>
     <item name="android:textColor">#ffffff</item>
 </style>
</resources>

I have tried to implement:

我试图实现:

 <item name="android:windowTranslucentStatus">true</item>

It causes the contents of my application to move up, start in the status bar area and be covered by the Action Bar.

它使我的应用程序的内容向上移动,从状态栏区域开始并被操作栏覆盖。

Without the translucent code

没有半透明代码

采纳答案by thunsaker

Update:Found this great article from Matt Gaunt(a Google Employee) on adding a Translucent theme to Android apps. It is very thorough and addresses some of the issues many people seem to be having while implementing this style: Translucent Theme in Android

更新:Matt Gaunt(Google 员工)中找到了这篇关于向 Android 应用程序添加半透明主题的精彩文章。它非常彻底,解决了许多人在实现这种风格时似乎遇到的一些问题:Android 中的半透明主题

Just add the following to your custom style. This prevents the shifting of the content behind the ActionBar and up to the top of the window, not sure about the bottom of the screen though.

只需将以下内容添加到您的自定义样式中即可。这可以防止内容在 ActionBar 后面移动到窗口顶部,但不确定屏幕底部。

<item name="android:fitsSystemWindows">true</item>

Credit: Transparent Status Bar System UI on Kit-Kat

图片来源:Kit-Kat 上的透明状态栏系统 UI

回答by hichris123

It looks like all you need to do is add this element to the themes you want a translucent status bar on:

看起来您需要做的就是将此元素添加到您想要半透明状态栏的主题中:

<item name="android:windowTranslucentStatus">true</item>

回答by Muhammad Aamir Ali

Add these lines in your main theme

在您的主题中添加这些行

<style name="AppTheme" parent="android:Theme.Holo.Light">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:fitsSystemWindows">true</item>
</style>

回答by Ravi Rawal

To change the color of the status bar to windowBackground

将状态栏的颜色更改为 windowBackground

 <item name="android:windowBackground">@color/window_bg</item>

To add the translucent effect to status and navigation bar

为状态栏和导航栏添加半透明效果

<item name="android:windowTranslucentStatus">true</item>

<item name="android:windowTranslucentNavigation">true</item>

回答by Paito

You're very close you just need to update your view background colors:

你非常接近你只需要更新你的视图背景颜色:

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffd060" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_light"

        <!-- your stuff here -->
    </LinearLayout>
</FrameLayout>

Specifying a global background color in your AppBaseTheme would also work but would likely cause a mess as it would end up been the default background color for everything.

在 AppBaseTheme 中指定全局背景色也可以,但可能会造成混乱,因为它最终会成为所有内容的默认背景色。