Android之沉浸式状态栏

大神写的两个ToolBar的库

StatusBarUtil

ImmersionBar

Sofia

这个特性是andorid4.4支持的,最少要api19才可以使用,低于4.4以下是不能修改的。下面介绍一下使用的方法,非常得简单:

public class MainActivity extends Activity {

    private ListView listview;

    private MyAdapter adapter;
    private static String[] list = new String[]{"全国省市区", "时间"};
    private static Class[] classes = new Class[]{ProvincialCityActivity.class, TimeActivity.class};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            //沉浸顶部状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            //沉浸底部虚拟键
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        listview = (ListView) findViewById(R.id.listview);
        adapter = new MyAdapter();
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                startActivity(new Intent(MainActivity.this, classes[i]));
            }
        });

    }

看布局:activity_main 布局

 
<?xml version="1.0" encoding="utf-8"?> <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:gravity="center_horizontal"  android:orientation="vertical"> <ListView  android:id="@+id/listview"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="#009959" /> </LinearLayout> 

看图片

 
 

大家看到了吧,文字和状态栏重叠在一起了,这肯定是不行的,此时需要添加下面的代码:

 
android:clipToPadding="true" android:fitsSystemWindows="true" 

activity_main 布局中添加

<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center_horizontal"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#009959"
        android:clipToPadding="true"
        android:fitsSystemWindows="true" />

</LinearLayout>

加入那两行以后,界面仍然会是沉浸式的,但状态栏那部分,就不会再重叠了,像加了padding一样,如下图:

 

相关函数:

 
  1. fitsSystemWindows, 该属性可以设置是否为系统 View 预留出空间, 当设置为 true 时,会预留出状态栏的空间。

  2. ContentView, 实质为 ContentFrameLayout, 但是重写了 dispatchFitSystemWindows 方法, 所以对其设置 fitsSystemWindows 无效。

  3. ContentParent, 实质为 FitWindowsLinearLayout, 里面第一个 View 是 ViewStubCompat, 如果主题没有设置 title ,它就不会 inflate .第二个 View 就是 ContentView。

再学一遍android:fitsSystemWindows属性 (qq.com)

SystemUI Flag详解及使用情景 - 简书 (jianshu.com)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值