Android透明状态栏和操作栏

Gui*_*lhE 93 android android-appcompat android-actionbar android-toolbar android-statusbar

我已经对这个主题做了一些研究,我找不到一个完整的解决方案,所以,一步一步,经过一些反复试验,我终于找到了如何实现这些结果:透明或有色ActionbarStatusbar.请看下面的答案.

Gui*_*lhE 234

我正在开发一个应用程序,当涉及到操作栏和状态栏自定义时,需要在所有设备中使用> = API14看起来类似.我终于找到了一个解决方案,因为我花了一点时间来分享它来拯救你的一些.我们首先使用appcompat-21依赖项.

透明Actionbar:

values/styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light">
...
</style>

<style name="AppTheme.ActionBar.Transparent" parent="AppTheme">
    <item name="android:windowContentOverlay">@null</item>
    <item name="windowActionBarOverlay">true</item>
    <item name="colorPrimary">@android:color/transparent</item>
</style>

<style name="AppTheme.ActionBar" parent="AppTheme">
    <item name="windowActionBarOverlay">false</item>
    <item name="colorPrimary">@color/default_yellow</item>
</style>
Run Code Online (Sandbox Code Playgroud)


values-v21/styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    ...
</style>

<style name="AppTheme.ActionBar.Transparent" parent="AppTheme">
    <item name="colorPrimary">@android:color/transparent</item>
</style>

<style name="AppTheme.ActionBar" parent="AppTheme">
    <item name="colorPrimaryDark">@color/bg_colorPrimaryDark</item>
    <item name="colorPrimary">@color/default_yellow</item>
</style>
Run Code Online (Sandbox Code Playgroud)

现在,您可以在这些主题中使用这些主题AndroidManifest.xml来指定哪些活动具有透明或彩色ActionBar:

    <activity
            android:name=".MyTransparentActionbarActivity"
            android:theme="@style/AppTheme.ActionBar.Transparent"/>

    <activity
            android:name=".MyColoredActionbarActivity"
            android:theme="@style/AppTheme.ActionBar"/>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 在此输入图像描述 在此输入图像描述 在此输入图像描述 在此输入图像描述

注意:在API> = 21中获取Actionbar透明度,您也需要获得Statusbar透明,否则将不会尊重您的颜色样式并保持浅灰色.



透明状态栏(仅适用于API> = 19):
这个非常简单,只需使用以下代码:

protected void setStatusBarTranslucent(boolean makeTranslucent) {
        if (makeTranslucent) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }
Run Code Online (Sandbox Code Playgroud)

但你会发现一个时髦的结果: 在此输入图像描述

Statusbar是因为当透明时布局将使用其高度.为了防止这种情况,我们只需要:

解决方案一:在布局视图容器中
添加此行android:fitsSystemWindows="true",以便在Actionbar旁边放置任何内容:

    ...
        <LinearLayout
                android:fitsSystemWindows="true"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            ...
        </LinearLayout>
    ...
Run Code Online (Sandbox Code Playgroud)

解决方案二:
在我们之前的方法中添加几行:

protected void setStatusBarTranslucent(boolean makeTranslucent) {
        View v = findViewById(R.id.bellow_actionbar);
        if (v != null) {
            int paddingTop = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? MyScreenUtils.getStatusBarHeight(this) : 0;
            TypedValue tv = new TypedValue();
            getTheme().resolveAttribute(android.support.v7.appcompat.R.attr.actionBarSize, tv, true);
            paddingTop += TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
            v.setPadding(0, makeTranslucent ? paddingTop : 0, 0, 0);
        }

        if (makeTranslucent) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }
Run Code Online (Sandbox Code Playgroud)

R.id.bellow_actionbar我们想要放置的任何内容的布局容器视图ID将在哪里Actionbar:

...
    <LinearLayout
            android:id="@+id/bellow_actionbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        ...
    </LinearLayout>
...
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

所以就是这样,它认为我不会忘记一些事情.在这个例子中我没有使用a Toolbar但我认为它会有相同的结果.这就是我自定义我的方式Actionbar:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        View vg = getActionBarView();
        getWindow().requestFeature(vg != null ? Window.FEATURE_ACTION_BAR : Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);
        setContentView(getContentView());

        if (vg != null) {
            getSupportActionBar().setCustomView(vg, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            getSupportActionBar().setDisplayShowCustomEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(false);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            getSupportActionBar().setDisplayUseLogoEnabled(false);
        }
        setStatusBarTranslucent(true);
    }
Run Code Online (Sandbox Code Playgroud)

注意:这是一个abstract class延伸ActionBarActivity
希望它有帮助!

  • 如果你想确保你的View不在透明状态栏下面,你应该真的使用[android:fitsSystemWindows](http://developer.android.com/reference/android/view/View.html#attr_android:fitsSystemWindows). (9认同)
  • 要获得透明StatusBar Works,我需要添加`FLAG_LAYOUT_NO_LIMITS`标志,将代码`getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);`之后`getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);` (6认同)
  • 你错过了`MyScreenUtils.getStatusBarHeight(this)`这里是答案http://stackoverflow.com/a/3410200/1307690 (4认同)