更新2:低版本实现Material Design 的两种方式
第三方库:com.readystatesoftware.systembartint
更新1 :发现了一个神奇方便的Utils for StatusBar 状态栏兼容类.
StatusBarCompat
values的dimens中
values中
<dimen name="toolBarPaddingTop">0dp</dimen>
<dimen name="toolBarHeight">50dp</dimen>
values-v19和values-v21中
<dimen name="toolBarPaddingTop">25dp</dimen>
<dimen name="toolBarHeight">75dp</dimen>
toolbar布局中
设置属性 android:fitsSystemWindows=”true”,此属性加在布局中不起作用,要加在布局中最靠近状态栏的第一个子控件中;
设置layout_height和paddingTop为我们设置好的
toolBarHeight,toolBarPaddingTop
<android.support.v7.widget.Toolbar
android:id="@+id/base_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/toolBarHeight"
android:background="@android:color/holo_blue_light"
android:fitsSystemWindows="true"
android:paddingTop="@dimen/toolBarPaddingTop"
>
<!--android:theme="@style/Theme.Drawer.ArrowAnimation"-->
设置activity的属性
有两种方法,代码实现或者style文件实现
1.NoTitle、
2.在android 4.4以上即api19以上的状态栏为半透明
3.将我们自己的toolbar替换掉actionbar
代码实现:
在BaseActivity的onCreate中
//设置noTitle
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
//设置android 4.4以上即api19以上的状态栏为半透明
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WindowManager.LayoutParams localLayoutParams = getWindow ().getAttributes ();
localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
}
//将我们自己的toolbar替换掉actionbar,最好让activity的theme是noActionbar的
setContentView(R.layout.activity_base_toolbar);
mToolbar = (Toolbar) findViewById(R.id.base_toolbar);
setSupportActionBar(mToolbar);
在style中设置
values中:
<style name="TranslucentTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
// <item name="colorPrimary">@color/colorPrimary</item>
// <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
// <item name="colorAccent">@color/colorAccent</item>
</style>
<style name="TranslucentTheme" parent="TranslucentTheme.Base">
values-v19中:
<!-- Base application theme. -->
<style name="TranslucentTheme" parent="TranslucentTheme.Base">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
values-v21中:
<!-- Base application theme. -->
<style name="TranslucentTheme" parent="TranslucentTheme.Base">
<!-- Customize your theme here. -->
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
<!--<item name="android:statusBarColor">@android:color/transparent</item>-->
</style>