【问题标题】:Taking advantage of the translucent status bar in Android 4.4 KitKat利用 Android 4.4 KitKat 中的半透明状态栏
【发布时间】:2014-01-01 22:43:06
【问题描述】:

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

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

我已经尝试实现了:

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

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

【问题讨论】:

    标签: android user-interface statusbar android-4.4-kitkat


    【解决方案1】:

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

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

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

    信用:Transparent Status Bar System UI on Kit-Kat

    【讨论】:

    • 另外值得注意的是,这位来自 Reddit 的互联网公民对如何将颜色放入状态栏有很好的解释。基本上,您使用所需的背景颜色将基本布局包装在 FrameLayout 中。 reddit.com/r/Android/comments/1ssgkt/…
    • 如果你在你的应用中使用 Toasts 这有点危险,见code.google.com/p/android/issues/detail?id=63653
    • 谢谢!很棒的文章
    • 答案很有用,去掉不相关的链接,这个链接指的是什么? Android中的半透明主题(gauntface.com/blog),指定具体内容的链接(为-1)
    【解决方案2】:

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

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

    【讨论】:

    • 我之前在我的 Styles.xml 中尝试过这个,我想知道是否还有更多内容。当我刚刚添加它时,它会导致我的应用程序的内容向上移动并在状态栏中启动并被操作栏隐藏。这是我实施后的示例图像:Without the translucent codeWith the translucent code.
    【解决方案3】:

    在您的主题中添加这些行

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

    【讨论】:

      【解决方案4】:

      你已经很接近了,你只需要更新你的视图背景颜色:

      <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 中指定全局背景颜色也可以,但可能会导致混乱,因为它最终会成为所有内容的默认背景颜色。

      【讨论】:

      • 但这不会产生额外的透支吗?
      【解决方案5】:

      将状态栏的颜色改为windowBackground

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

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

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

      【讨论】:

        猜你喜欢
        • 2014-03-18
        • 1970-01-01
        • 2015-08-06
        • 2017-07-07
        • 1970-01-01
        • 1970-01-01
        • 2014-12-15
        • 2015-04-07
        相关资源
        最近更新 更多