Android开发 之 全屏显示布局(相关主题,状态栏,布局填充)以及状态栏字体的颜色

本文探讨了在Android开发中如何实现全屏显示布局,包括填充状态栏和导航栏,并着重讲解了在Android 6.0及以上版本如何改变状态栏的默认白色字体颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局填充状态栏和导航栏

1.创建项目时,项目主题如下:默认带有actionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
       
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/tabbackground</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
去掉这个actionbar更换主题有两种:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/tabbackground</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
或者
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="windowNoTitle">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/tabbackground</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

2.透明状态栏是4.4开始有的,只需在主题中加入以下属性节点:
4.4添加:
        <item name="android:windowTranslucentStatus">true</item>//透明状态栏
        <item name="android:windowTranslucentNavigation">true</item>//透明导航栏

5.x添加:
        <item name="android:windowTranslucentStatus">true</item>//5.x以后这个属性变成了半透明了
        <item name="android:windowTranslucentNavigation">true</item>//透明导航栏
        <item name="android:statusBarColor">@android:color/transparent</item>//5.x以后设置全透明
3.隐藏状态栏:两种方法
主题节点节点加入
<item name="android:windowFullscreen">true</item>

或者在代码中加入
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

实现以上三步就ok了。
 

4. android 6.0以后;系统默认是白色状态栏字体颜色;

    /**
     * Android 6.0 以上设置状态栏颜色
     */
    protected void setStatusBar(@ColorInt int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            // 设置状态栏底色颜色
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().setStatusBarColor(color);

            // 如果亮色,设置状态栏文字为黑色
            if (isLightColor(color)) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            }
        }

    }

    /**
     * 判断颜色是不是亮色
     *
     * @param color
     * @return
     *
     */
    private boolean isLightColor(@ColorInt int color) {
        return ColorUtils.calculateLuminance(color) >= 0.5;
    }

    /**
     * 获取StatusBar颜色,默认白色
     *
     * @return
     */
    protected @ColorInt int getStatusBarColor() {
        return Color.WHITE;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值