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

22

当我发布我的笔记应用程序适用于Android 4.0-4.3时,我使用了自定义的操作栏颜色和自定义的操作栏图标(而不是使用标准的浅色和深色操作栏)。我希望在Android 4.4上,状态栏也能采用我在Action Bar中使用的自定义颜色(即#FFD060)。是否有一种简单的方法可以更改我的当前styles.xml文件,以便4.4可以利用这一点?

我的styles.xml文件位于值-v19文件夹下,如下所示:

<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>

我已经尝试实现:

 <item name="android:windowTranslucentStatus">true</item>
它导致我的应用程序内容向上移动,从状态栏区域开始,并被操作栏覆盖。 没有半透明代码的情况
5个回答

15
更新:我发现了一篇来自Matt Gaunt(一位Google员工)的优秀文章,讲述了如何为Android应用添加半透明主题。这篇文章非常详细,解决了许多人在实现此风格时遇到的问题:Android中的Translucent Theme

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

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

来源: 在Kit-Kat上实现透明状态栏系统UI


值得注意的是,来自Reddit的这位互联网公民有一个很好的解释,说明如何将颜色放入状态栏。基本上,您可以在带有所需背景颜色的FrameLayout中包装您的基本布局。 - thunsaker
10
如果你的应用程序中使用了 Toasts,这可能有一点危险,请参阅https://code.google.com/p/android/issues/detail?id=63653。 - Paito
谢谢!非常棒的文章。 - Matias Elorriaga
答案很有用,删除无关链接,这个链接是关于什么的?Android中的半透明主题(https://gauntface.com/blog),请指定到具体内容的链接(-1)。 - Sackurise

2
在您的主题中添加以下代码行:
<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>

2
看起来你只需要将这个元素添加到你想要半透明状态栏的主题中即可:
<item name="android:windowTranslucentStatus">true</item>

我之前尝试在Styles.xml中实现这个效果,但我想知道是否还有其他的方法。当我只添加这段代码时,它会导致我的应用程序内容向上移动并从状态栏开始,并被操作栏隐藏。以下是我实现后的示例图像: 没有半透明代码有半透明代码 - Amonstan

0

将状态栏的颜色更改为windowBackground

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

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

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

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

0

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

<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>

在AppBaseTheme中指定全局背景颜色也可以起作用,但很可能会造成混乱,因为它最终会成为所有东西的默认背景颜色。

2
但这难道不会产生额外的超绘吗? - FD_

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