【问题标题】:Android, How to make a transparent status bar correctlyAndroid,如何正确制作透明状态栏
【发布时间】:2014-07-14 14:24:23
【问题描述】:

我正在尝试在 Android 4.4+ 上制作透明状态栏 我知道如何通过定义自己的风格来使其透明:

<style name="Theme.MyTheme" parent="Theme.MyTheme.Base">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item> </style>

我在这里找到: https://stackoverflow.com/a/20573595/2633630

问题是当状态栏是透明的时,它与动作栏的颜色不匹配,并且看到了后台的活动:

所以我发现我可以在我在这里找到的布局中使用 android:fitsSystemWindows="true" 和 android:clipToPadding="false" http://mindofaandroiddev.wordpress.com/2013/12/28/making-the-status-bar-and-navigation-bar-transparent-with-a-listview-on-android-4-4-kitkat/

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_darkRed"
android:fitsSystemWindows="true"
android:clipToPadding="false"    
tools:context="com.sandak.......">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"> 
....

这解决了透明状态栏的顶部问题,看起来还可以。不幸的是,系统按钮的“导航栏”底部有问题,它是红色的,并且我希望它不透明:

我尝试了所有可能的变体来实现工作结果,但我不知道如何解决它。我唯一可行的解​​决方案是将顶部填充设置为根元素约 60dp。但是这个解决方案很丑陋,而且在不同的设备上看起来可能会有所不同,并且可能无法正常工作。

我在 Google Play 上发现了一些应用程序,它们在透明背景下运行良好,所以我很好奇它们是如何使其工作的。

例如: https://play.google.com/store/apps/details?id=com.trello 和其他一些人

【问题讨论】:

  • 当我像你一样将 android:windowTranslucentStatus/Navigation 设置为 true 时,背景只是半透明的。你有没有做任何不同的事情来获得你在截图中的那种漂亮的暗渐变效果?

标签: android android-layout user-experience


【解决方案1】:

//更新:
好的,这是很老的答案。现在您可以使用 Material 主题来处理这个问题,这非常容易。只需将目标 api 设置为 21+,如果需要,使用支持库并使用材料主题创建主题,您可以直接指定状态栏颜色

<style name="AppTheme"
        parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">@color/color_primary</item>

        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">@color/color_primary_dark</item>

        <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
        <item name="colorAccent">@color/accent_orange</item>
    </style>

================================================ =========================

旧答案:

好的,看来我解决了我的问题。它不是最好和最清晰的解决方案,但它是有效的解决方案。 如果将来有人会找到更好,更清晰的解决方案,请告诉我,我将更新我的答案/或将您的答案标记为正确答案。但就目前而言,我没有比这个小技巧更好的解决方案了:

布局的根元素必须是RelativeLayout。应该遵循您的主要布局,如 ScrollView 或任何其他布局,没关系。在关闭相对布局标签之前的最后,后面是空的 LinearLayout,它设置了与操作栏相同的背景和固定高度(状态栏的高度)。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sandak....">
<ScrollView
    android:fitsSystemWindows="true"
    android:clipToPadding="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
.... all your main views etc..
</ScrollView>
<LinearLayout
        android:id="@+id/statusBarBackgroundLinearLayout"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="30dp"
        android:background="@color/background_darkRed"
        android:clickable="false"
        android:focusable="false">
     </LinearLayout>
</RelativeLayout>

好的,那么您必须在代码中设置与状态栏相同的线性布局高度:

private void setStatusBarBackground(View rootView) {
        setStatusBarLayout(rootView);
        statusBarHeight = getStatusBarHeight();
        setStatusBarLayoutHeight(statusBarHeight);
    }

    private void setStatusBarLayout(View rootView){
        statusBarBackgroundLinearLayout = (LinearLayout) rootView.findViewById(R.id.statusBarBackgroundLinearLayout);
        if (isPreKitkatDevice()) {
            hideStatusBarLayout();
        }
    }

    private int getStatusBarHeight() {
        int statusBarHeight = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight;
    }

    private boolean isPreKitkatDevice(){
        return Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT;
    }

    private void hideStatusBarLayout(){
        statusBarBackgroundLinearLayout.setVisibility(View.INVISIBLE);
    }        

    private void setStatusBarLayoutHeight(int height){
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) statusBarBackgroundLinearLayout.getLayoutParams();
        layoutParams.height = height;
    }

然后只需在 onCreate() 方法中调用 setStatusBarBackground。这是适用于所有不同类型设备的有效解决方案。对于不允许透明背景的前 KitKat 设备,您必须隐藏线性布局。

【讨论】:

  • getStatusBarLayout(View v) 没有返回类型?您可以更改该名称。 :)
猜你喜欢
  • 1970-01-01
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
相关资源
最近更新 更多