原文链接 :点击打开链接
Android 5.0的Material Design酷炫、吊炸天,先来解决黑黑的手机状态栏,再一步一步实现Material Design。
对于Android 5.0 Status bar和App bar的背景色可以轻松定义,对于Android 4.4如何实现呢?
来两张效果图,那张更清晰、好看?
《—–》
Status bar
On Android, the status bar contains notification icons and system icons.
Android status bar height: 24dp
ps:Android navigation bar 会引发一些问题,暂时不写了。
预备基础知识(Translucent system bars)
android:fitsSystemWindows
布局时是否考虑给系统窗口留出空间,true为留出空间(如 Status Bar and App bar)
android:clipToPadding
是否允许ViewGroup在padding中绘制,该值默认为true,即允许,padding无效;false:padding生效.
透明系统栏设置(Status bar 和 Navigation bar)
1
2
|
Theme.Holo.NoActionBar.TranslucentDecor
Theme.Holo.Light.NoActionBar.TranslucentDecor
|
如果设置了透明系统栏就必须设置fitsSystemWindows:true和android:clipToPadding:false(适用于listview等)留出系统栏空间而不会被遮挡.
也可以在自定义主题中,增加windowTranslucentNavigation和windowTranslucentStatus属性。
Activity,Window和View之间的关系
1
2
3
|
//了解 下面方法是获取 rootView
Window win = activity.getWindow();
ViewGroup decorViewGroup = (ViewGroup) win.getDecorView();
|
SystemBarTint 项目
Apply background tinting to the Android system UI when using KitKat translucent modes.
1
2
3
|
//只对4.4做修改~修改代码:Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
if
(Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT){
}
|
设置4.4的Status bar颜色
第一步:定义主题Theme
values/
values-v19/
values-v21/
1
2
3
4
5
|
<
style
name
=
"AppNavigationDrawerTheme"
parent
=
"@android:style/Theme.Holo.Light"
>
<!--Translucent system bars-->
<
item
name
=
"android:windowTranslucentNavigation"
>true</
item
>
<
item
name
=
"android:windowTranslucentStatus"
>false</
item
>
</
style
>
|
第二步:设置布局属性
//同样只对values-v19/设置
1
2
|
android:fitsSystemWindows="@bool/fitsSystemWindows"
android:clipToPadding="false"
|
第三步:设置颜色值
//同样只对Api19生效
//Activity
1
2
3
4
5
6
7
8
9
|
@TargetApi
(
19
)
public
static
void
setTranslucentStatus(Activity activity,
boolean
on) {
if
(Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
SystemBarTintManager tintManager =
new
SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(
true
);
tintManager.setNavigationBarTintEnabled(
false
);
tintManager.setStatusBarTintColor(activity.getResources().getColor(R.color.colorPrimary));
}
}
|
//ActionBarActivity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@TargetApi
(
19
)
public
void
setTranslucentStatus(Activity activity,
boolean
on) {
if
(Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
Window win = activity.getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final
int
bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if
(on) {
winParams.flags |= bits;
}
else
{
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
SystemBarTintManager tintManager =
new
SystemBarTintManager(activity);
tintManager.setStatusBarTintEnabled(
true
);
tintManager.setNavigationBarTintEnabled(
true
);
tintManager.setStatusBarTintColor(activity.getResources().getColor(R.color.colorPrimary));
tintManager.setNavigationBarTintColor(activity.getResources().getColor(R.color.colorPrimary));
tintManager.setStatusBarTintResource(R.color.colorPrimary);
}
}
|
搞定收工
GitHub地址:https://github.com/captain-miao/AndroidStartupDemo
apk下载地址:http://fir.im/yan
遇到的问题
- 不要将fitsSystemWindows放在theme中,将影响所有控件:fitsSystemWindows 设置为true,Toast文本显示偏上方什么鬼?