我们都知道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