我們都知道IOS上的app和上面的狀態欄是一個顏色的,給人一種渾然一體的感覺.看起來很舒服,那麼.在android上這個功能是怎麼實現的呢?下面給大家介紹一種簡單的方法來實現這種效果
先上效果圖:
我們的佈局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff6600"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#ff6600" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="中間標題"
android:textColor="#fff" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
</LinearLayout>
</LinearLayout>
其中最重要的三個元素就是根佈局的clipToPadding fitsSystemWindows 和background了,background的顏色就是狀態欄的顏色了
在我們的actiivty裏面,我們需要將actionbar去掉,也要設置一些window的狀態
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
setImmersionStatus();
}
private void setImmersionStatus() {
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
// 透明狀態欄
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 透明導航欄
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
}
到此我們的狀態欄顏色就已經修改了.that's all ,thx