对状态栏(Status Bar)和下方导航栏(Navigation Bar)进行半透明处理

来源:互联网 发布:软件技术服务保障计划 编辑:程序博客网 时间:2024/06/18 12:24

转载自Android 4.4+ 实现半透明状态栏(Translucent Bars) - CSDN博客 http://blog.csdn.net/leejizhou/article/details/48232015,亲自尝试了一下,感觉还不错

从原作者那里盗的图片(我的测试机没有下方导航栏,只有顶部状态栏,亲测顶部状态栏效果就如作者显示的一模一样):

下面说说如何使用这种效果:

1:在onCreate里面代码设置半透明的属性,由于只有Android 4.4以上才支持这种效果,所以代码需要判断下

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            //透明状态栏            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);            //透明底部导航栏            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);         }

2:在这个界面上我去掉了Actionbar,实现方式有很多,这里我使用的是在Style里去掉。

  <!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item>    </style>

3:这个部分需要留意一下,如果希望APP的显示内容正常和滚动透明化需要加上android:fitsSystemWindows=”true”和android:clipToPadding=”false”的属性,建议你把这两个属性好好试试加上与否的区别。

<ScrollView 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:fitsSystemWindows="true"    android:clipToPadding="false"    android:background="#795548"    tools:context=".DefaultActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="18sp"        android:textColor="#ffffff"        android:text="@string/str" /></ScrollView>

(这两个属性我设置前后,没有发现任何区别,不知道是不是我没观察得仔细。。。)
这样一个简单的半透明化效果就实现了

阅读全文
0 0