Android中沉浸式状态栏,改变状态栏颜色类似QQ样式

来源:互联网 发布:拜发试剂盒软件 编辑:程序博客网 时间:2024/06/06 01:50

1、使用该功能需要使用到第三方库SystemBarTint

下载地址:https://github.com/jgilfelt/SystemBarTint


2、需要在AndroidMainfest文件中Application设置样式

android:theme="@android:style/Theme.Light.NoTitleBar"

3、第一种调用方法,常用调用方法,效果与QQ有些差异


java代码:
@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_match_actionbar);                //只对api19以上版本有效        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            setTranslucentStatus(true);        }                //为状态栏着色        SystemBarTintManager tintManager = new SystemBarTintManager(this);        tintManager.setStatusBarTintEnabled(true);        tintManager.setStatusBarTintColor(Color.RED);    }    @TargetApi(19)     private void setTranslucentStatus(boolean on) {        Window win = getWindow();        WindowManager.LayoutParams winParams = win.getAttributes();        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;        if (on) {            winParams.flags |= bits;        } else {            winParams.flags &= ~bits;        }        win.setAttributes(winParams);}

效果图:


状态栏将布局挡住了,在布局最外层Layou中加上:
android:fitsSystemWindows="true"

效果图:


4、第二种方法,类似于QQ的效果


java代码:
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 只对api19以上版本有效if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {setTranslucentStatus(true);}}@TargetApi(19)private void setTranslucentStatus(boolean on) {Window win = getWindow();WindowManager.LayoutParams winParams = win.getAttributes();final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;if (on) {winParams.flags |= bits;} else {winParams.flags &= ~bits;}win.setAttributes(winParams);}

XML代码:
<RelativeLayout 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:background="#FF0000"    android:fitsSystemWindows="true"    android:paddingTop="@dimen/activity_vertical_margin" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:background="#FFFFFF"        android:orientation="vertical" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/hello_world" />    </LinearLayout></RelativeLayout>

在布局最外层设置    

android:background="#FF0000"android:fitsSystemWindows="true"

背景设置在下层Layout,不然就整体颜色一致了,这样设置状态栏与整个界面为一个整体,页面滑动状态栏也会跟随滑动。



效果图:


0 0
原创粉丝点击