-
5.0以上的处理:
自5.0引入 Material Design ,状态栏对开发者更加直接,可以直接调用 setStatusBarColor 来设置状态栏的颜色.在oncreat方法中直接调用
getWindow().setStatusBarColor(getResources().getColor(R.color.color_1f2326));即可
- 一般我们采用下面方法直接设置为透明,无论是5.0以上还是5.0以下都可以适配但是要注意的事在布局文件中加入以下两行
- android:fitsSystemWindows="true"
- android:clipToPadding="true"
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //透明状态栏
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- //透明导航栏
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
- }
- }
- 在布局文件中直接如下
-
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:fillViewport="true" android:focusable="true" android:focusableInTouchMode="true" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/color_white" > <LinearLayout android:fitsSystemWindows="true" android:clipToPadding="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/color_1f2326" android:orientation="vertical"> <LinearLayout android:layout_marginBottom="@dimen/px20" android:layout_width="match_parent" android:layout_height="@dimen/px70" android:orientation="horizontal" >
-