Android_沉浸式状态栏

来源:互联网 发布:申请美国博士知乎 编辑:程序博客网 时间:2024/05/16 05:58

转载清标明出处:http://blog.csdn.net/sh_12345/article/details/52919894

这个特性是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。

扩展:

地址:http://blog.csdn.net/jdsjlzx/article/details/41643587

3 0
原创粉丝点击