为什么我们不能使用带有和ActionBar的半透明系统条

时间:2013-11-02 21:12:58

标签: android android-actionbar android-4.4-kitkat

在将我的应用程序更新为Kitkat时,我只想使用Translucent属性在KitKat上给它们一个漂亮的外观:

  

半透明系统栏

     

现在,您可以使用新主题Theme.Holo.NoActionBar.TranslucentDecor和Theme.Holo.Light.NoActionBar.TranslucentDecor使系统栏部分半透明。通过启用半透明系统条,您的布局将填充系统条后面的区域,因此您还必须为布局中不应被系统条覆盖的部分启用[fitsSystemWindows] [4]。

我唯一担心的是我想使用一个与Google想要相反的ActionBar(两个主题都有NoActionBar:

Theme.Holo.NoActionBar.TranslucentDecor 
Theme.Holo.Light.NoActionBar.TranslucentDecor 

由于我不打算使用一些黑客或技巧来使其发挥作用,我只是想知道是否有一些正确的方法来实现这一目标,或者这是否违反Google指南。

enter image description here

5 个答案:

答案 0 :(得分:48)

您可以创建自己的主题,android.R.attr#windowTranslucentStatus设置为true,以达到相同的效果而不会丢失ActionBar

来自文档

  

指示此窗口是否请求半透明状态栏的标志。   对应                至{@link android.view.WindowManager.LayoutParams #FLAG_TRANSLUCENT_STATUS}。

样式 - 请记住,这些内容会放在values-v19

<style name="TranslucentStatusBar" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/TranslucentActionBar</item>
    <item name="android:windowTranslucentNavigation">false</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

<style name="TranslucentActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@android:color/transparent</item>
</style>

<强>布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_dark"
    android:fitsSystemWindows="true" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_purple" />

</FrameLayout>

<强>结果

enter image description here

答案 1 :(得分:40)

这适用于较少的行:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow(); // in Activity's onCreate() for instance
        w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }

如果有人可以添加,如何检查半透明导航是否可用,那将是很好的,因为N10例如将使用Build.VERSION_CODES.KITKAT禁用半透明导航。 编辑:在这个问题中回答: Check if translucent navigation is available

答案 2 :(得分:29)

以下代码在我的应用程序中运行良好。

透光性:

Window w = getWindow();
                w.setFlags(
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                w.setFlags(
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

设置不是半透明的:

final WindowManager.LayoutParams attrs = getWindow()
                        .getAttributes();
                attrs.flags &= (~WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                attrs.flags &= (~WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                getWindow().setAttributes(attrs);
                getWindow().clearFlags(
                        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

答案 3 :(得分:17)

这是我正在寻找的答案: https://github.com/jgilfelt/SystemBarTint

  Android 4.4(KitKat)推出了半透明系统UI样式   状态和导航栏。这些款式非常适合壁纸   主屏幕发射器等活动,但背景最小   提供的保护使它们对其他类型的活动不太有用   除非您在布局中提供自己的背景。决定   给定设备的系统UI的大小,位置和存在   配置可以是非平凡的。

     

这个库提供了一种为其创建背景“色调”的简单方法   系统条,无论是颜色值还是Drawable。默认情况下它会给出   你是一个半透明的黑色背景,可用于全流血   持久系统UI仍然很重要的内容屏幕 - 比如   放置在地图或照片网格上时。

enter image description here

答案 4 :(得分:4)

所有答案都以编程方式或布局扩展ActionBar:这意味着当应用程序启动或重新创建时,系统将查找主题(不知道修复)并显示非常丑陋状态栏用任何窗口背景着色......!

我创建了一个如何使用主题

进行操作的示例
  

https://github.com/Takhion/android-extendedactionbar

我希望它对某人有用:)