沉浸式状态栏
来源:互联网 发布:网络视频营销 编辑:程序博客网 时间:2024/05/19 19:40
沉浸式状态栏只支持安卓4.4及以上的版本
状态栏:4.4上是渐变色,5.0上是完全透明。
第一种场景:
1.首先在values、values-v19、values-v21文件夹下的styles.xml都设置一个 新的style
values/styles
<style name="CustomTheme" parent="AppTheme"> <!--在Android 4.4之前的版本上运行,直接跟随系统主题--> </style>
values-v19/styles
<style name="CustomTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:windowTranslucentStatus">true </item> <item name="android:windowTranslucentNavigation">true </item> </style>
values-v21/styles
<style name="CustomTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:windowTranslucentStatus">true </item> <item name="android:windowTranslucentNavigation">true </item> <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色--> <item name="android:statusBarColor">@android:color/transparent </item> </style>
2.给 activity 设置theme属性值为@style/CustomTheme
<activity android:name=".MainActivity" android:theme="@style/CustomTheme"> </activity>
3.去除标题栏
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
使用纯代码实现:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//WindowManager.LayoutParams.FLAG_TRANSLUCE 使状态栏半透明 getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);} //去除标题栏 //继承AppCompatActivity使用 supportRequestWindowFeature(Window.FEATURE_NO_TITLE); //继承Activity使用 // requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色,手动设置状态栏颜色 getWindow().setStatusBarColor(Color.TRANSPARENT); }
效果图
这里要注意如果你此时在R.layout.activity_main布局文件中设置fitsSystemWindows=“true”
效果图
fitsSystemWindows=“false”
效果图
System windows指的就是屏幕上status bar 、 navigation bar等系统控件所占据的部分。而android:fitsSystemWindows=”true”就是通过设置View的padding,使得应用的content部分(Activity中setContentView()中传入的view)不会与system window重叠。
第二种场景把状态栏颜色和App的导航栏颜色一样。
第一步: 状态栏透明化; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); }
第二步: 去除标题栏 //继承AppCompatActivity使用 supportRequestWindowFeature(Window.FEATURE_NO_TITLE); //继承Activity使用 // requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main);
第三步: sdk版本5.0以上直接调用 getWindow().setStatusBarColor(color)设置颜色。 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (statusColor != INVALID_VAL) { getWindow().setStatusBarColor(statusColor); } return; }//sdk版本5.0以下Window对象没有setStatusBarColor()方法。所以这里创建一个与状态栏高度相同的view,李代桃僵,给他设置颜色来达到效果。 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { int color = COLOR_DEFAULT; ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content); if (statusColor != INVALID_VAL) { color = statusColor; } View statusBarView = new View(this); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(this)); statusBarView.setBackgroundColor(color); contentView.addView(statusBarView, lp); }第四步: 布局文件添加fitsSystemWindows=“true”,结束
阅读全文
0 0
- 状态栏、沉浸式状态栏
- 沉浸式状态栏/透明状态栏
- 透明状态栏(沉浸式状态栏)
- Android 沉浸式状态栏
- 沉浸式状态栏
- android 沉浸式 状态栏
- android 沉浸式状态栏
- android沉浸式状态栏
- 沉浸式状态栏
- Android沉浸式状态栏
- Android 沉浸式状态栏
- 沉浸式状态栏
- 沉浸式状态栏
- android 沉浸式状态栏
- Android沉浸式状态栏
- 设置沉浸式状态栏
- Android沉浸式状态栏
- 沉浸式状态栏
- java打jar包需要注意的事项
- VMware虚拟磁盘VMDK格式说明书1.1---3 The Descriptor File描述文件
- Appium新版本不再支持ByName定位了怎么办
- HttpClient androidStudio中的基础使用
- SQLCipher的异常总结
- 沉浸式状态栏
- 图片转PDF,图片过长智能截取
- 2017中国国际教育装备博览会会刊(参展商名录)
- Java线程相关知识
- maven 打包 时出现非法字符: /65279错误
- apache 配置 404页面
- Android 网络框架 Retrofit2.0介绍、使用和封装
- TreeMap
- HttpClient androidStudio基本的使用方法