您的位置:首页 > 移动开发 > Android开发

Android4.4新的特性,在应用内开启透明状态栏和透明虚拟按钮。

2015-06-20 00:00 471 查看
除了沉浸模式外,Android 4.4还有新的API,能使应用内的状态栏和虚拟按钮透明。其他更多的Android 4.4 APIs可以看这里
如果要使应用内的状态栏和虚拟按钮变成透明有两种方法。
一种是代码方式:
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);


另外一种是使用两个新的主题风格:
Theme.Holo.NoActionBar.TranslucentDecorTheme.Holo.Light.NoActionBar.TranslucentDecor
但是这种方式只支持Android4.4以上系统,所以为了保持兼容性,我们还是采用代码方式比较好。只需要先判断,如果是4.4以上系统才启用代码。
开启后上下系统栏都透明了。



但是如果应用本身是带有actionbar或者标题栏的话会就会变得比较尴尬,内容会在上面露出来。这个时候需要在布局文件里加入android:fitsSystemWindows="true"。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#c8c8c8" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>


加入前 加入后





但是这样的话内容就不能从透明的虚拟按钮下穿过,没原来那么好看。我们可以按照以前一样把根布局设置一个高度为系统栏高度和ActionBar高度的内边距就可以。
同时关于获取ActionBar和状态栏的高度,发现用原来的方法有时候会获取的值为0。自己google找了一下,找到两个前辈提供的获取高度方法,获取ActionBar高度获取状态栏高度
if (android.os.Build.VERSION.SDK_INT > 18) {
Window window = getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//设置根布局的内边距
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.layout);
relativeLayout.setPadding(0, getActionBarHeight()+getStatusBarHeight(), 0, 0);
}


// 获取手机状态栏高度
public int getStatusBarHeight() {
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, statusBarHeight = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return statusBarHeight;
}
// 获取ActionBar的高度
public int getActionBarHeight() {
TypedValue tv = new TypedValue();
int actionBarHeight = 0;
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))// 如果资源是存在的、有效的
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
}
return actionBarHeight;
}


设置后的效果



接下来,因为我自己写的一些应用是暗色的主题的,会导致透明的状态栏和ActionBar颜色不太协调。看到有一些应用是把状态栏的颜色设置成和ActionBar一样,这种解决方法也不错。
具体是怎么实现的也不太清楚,我自己猜测写了一个差不多状态栏。我是直接在根视图加入一个高度为状态栏高度的TextView,背景设置为和ActionBar一样。具体代码如下:
// 创建TextView
TextView textView = new TextView(this);
LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, getStatusBarHeight());
textView.setBackgroundColor(Color.parseColor("#3F9FE0"));
textView.setLayoutParams(lParams);
// 获得根视图并把TextView加进去。
ViewGroup view = (ViewGroup) getWindow().getDecorView();
view.addView(textView);




在模拟器上看还行,但是在实际的手机当中总感觉ActionBar有点过大,所以我在背景色里加入了一些渐变,在实体手机中就比较好看一点,不会觉得ActionBar太宽了。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:startColor="#c8c8c8"
android:endColor="#3F9FE0"
android:angle="270"
android:type="linear"/>
</shape>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息