现在的位置: 首页 > 综合 > 正文

android 4.4 支持透明状态栏和透明导航栏

2018年01月10日 ⁄ 综合 ⁄ 共 1347字 ⁄ 字号 评论关闭

Google 在 4.4 给全屏阅读文字或玩游戏这种情景增加了透明状态栏和透明导航栏的功能


方法1:设置 Acitivity 所在 window 的属性

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        setContentView(R.layout.activity_main);

    }


如果 Activity 有 actionbar,那么还需要在 Activity 的布局文件的根节点上设置两个属性

<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:clipToPadding="true"
    android:fitsSystemWindows="true"


    android:orientation="vertical"
    tools:context="com.example.statusbar.MainActivity" >


方法2:设置 theme 属性

android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar.TranslucentDecor"
android:theme="@android:style/Theme.Holo.Light.NoActionBar.TranslucentDecor"

android:theme="@android:style/Theme.Holo.NoActionBar.TranslucentDecor"

如果使用自定主题,只需在在 values-19 文件夹下添加以下属性

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"
>
    <!-- API 19 theme customizations can go here. -->
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>

效果如下图所示:


抱歉!评论已关闭.