Android透明状态栏和操作栏

时间:2015-04-27 23:15:32

标签: android android-actionbar android-toolbar appcompat-v7-r21 android-statusbar

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

3 个答案:

答案 0 :(得分:230)

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

透明操作栏

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


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

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

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

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

enter image description here enter image description here enter image description here enter image description here enter image description here

注意:在API&gt; = 21中要使Actionbar透明,您需要使Statusbar透明,否则将不会尊重您的颜色样式并保持浅灰色。



透明状态栏(仅适用于API&gt; = 19)
这个很简单,只需使用以下代码:

protected void setStatusBarTranslucent(boolean makeTranslucent) {
        if (makeTranslucent) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

但你会发现一个时髦的结果: enter image description here

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

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

    ...
        <LinearLayout
                android:fitsSystemWindows="true"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            ...
        </LinearLayout>
    ...

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

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);
        }
    }

其中R.id.bellow_actionbar将是我们想要放置的任何内容的布局容器视图ID Actionbar

...
    <LinearLayout
            android:id="@+id/bellow_actionbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        ...
    </LinearLayout>
...

enter image description here

所以就是这样,它认为我没有忘记一些事情。 在这个例子中,我没有使用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);
    }

注意:这是一个abstract class,其范围为ActionBarActivity 希望它有所帮助!

答案 1 :(得分:1)

它支持KITKAT之后。只需在Activity的onCreate方法中添加以下代码即可。无需对Manifest文件进行任何修改。

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

答案 2 :(得分:0)

只需将以下代码行添加到您的活动/片段Java文件中即可:

getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
);