1. 程式人生 > >Android 沉浸式狀態列效果(狀態列與內容完全融合、類似於iphone 7.0系統)

Android 沉浸式狀態列效果(狀態列與內容完全融合、類似於iphone 7.0系統)

今天偶然間發現一種叫做沉浸式狀態列的、看了之後激動的非常、立馬開操!

Activity的setContentView之前新增

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明狀態列
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明導航欄
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

layout最外層中新增:

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

下面是樓主的demo:

import com.example.cjstype.R;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明狀態列
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明導航欄
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
		setContentView(R.layout.activity_main);
	}
}

<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:clipToPadding="true"
    android:background="@drawable/qc" 
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >


</RelativeLayout>

怎麼樣,是不是很簡單!

特別注意:僅支援4.4及以上系統