今天要记录的是Android沉浸式状态栏的开发,看到别的应用都有沉浸式状态栏,是不是感觉很爽,今天我也来做一个沉浸式状态栏的例子,如下图所示:
哇擦,图好大,就不改了。。。
下面记录一下实现过程:
1、首先在需要使用沉浸式状态栏的Activity里,加上如下代码:
package com.example.translucentstatusbar;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.view.WindowManager;
public class MainActivity extends Activity {
@SuppressLint("InlinedApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//判断当前SDK版本号,如果是4.4以上,就是支持沉浸式状态栏的
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
}
主要是onCreate方法中的两句代码。
2、修改Activity对应的布局文件,加上两个属性,如下代码所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:orientation="vertical" >
</LinearLayout>
关键是clipToPadding和fitsSystemWindows这两个属性,如果不加这两个属性,你会看到沉浸式的状态栏,但是Activity中的内容会整体上移状态栏高度的距离,
3、如果你的AndroidManifest.xml文件中给Activity设置的主题带有ActionBar,或者有titleBar,那也需要改一下,设置下面的theme吧:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar" >
</application>
有了上面三个地方的配置,基本上就没问题了,沉浸式的状态栏就完成了,下面美化一下界面,给出MainActivity整个的布局文件
,如下代码所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:background="#6699ff"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:text="标题"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textSize="16sp"
android:textColor="#ffffff"
android:text="返回"
android:background="#00000000"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textSize="16sp"
android:textColor="#ffffff"
android:text="设置"
android:background="#00000000"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:textColor="#000000"
android:text="Hello World"
android:textStyle="italic"
/>
</LinearLayout>
</LinearLayout>
大功告成了,沉浸式状态栏的界面出来了,确实比黑底的状态栏好看多了!!!