android 透明状态栏方法及其适配键盘上推(二)
在上一篇文章中介绍了一种设置透明状态栏及其适配键盘上推得方法。但是上一篇介绍的方法中有个缺点,就是不能消除掉statusbar的阴影。很多手机如(三星,Nexus都带有阴影)。即使我用了:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp"
android:background="@android:color/transparent"
android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/transparent"
app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout>
还是不能消除阴影。那怎么办呢?这里介绍一种我觉得还不错的方法,思路是:不用toolbar,直接通过代码设置界面为透明statusbar,在每个界面里增加一个statusbar高度的view用来适配界面的显示,否则界面最顶端会有部分被statusbar覆盖。
方法如下:
1. 修改您应用里每个界面里的最顶端的布局:
<RelativeLayout
android:id="@+id/total_tile_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <RelativeLayout
android:id="@+id/actionbar_tile_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout> <!--titlebar的其他布局-->
</RelativeLayout>
2. 在activity的onCreate方法中(最好写在baseActivity里),增加如下代码,设置为透明statusbar同时清除背景阴影。
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//透明状态栏
// 状态栏字体设置为深色,SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 为SDK23增加
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); // // 部分机型的statusbar会有半透明的黑色背景
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().setStatusBarColor(Color.TRANSPARENT);// SDK21
3.同时在activity的onCreate方法中(最好写在baseActivity里),调用initMarginTopWithStatusBarHeight方法
/**
* 设置view的margintop高度为statusbar高度
* @param view 就是上面的 actionbar_tile_bg 这个view
* @param context
*/
public static void initMarginTopWithStatusBarHeight(View view,Context context){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null){
view.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(view.getLayoutParams());
rl.topMargin = getStatusBarHeight(context);
view.setLayoutParams(rl);
}else{
view.setVisibility(View.GONE);
}
}
这样就可以实现6.0以上的机型statusbar为透明且没有阴影(6.0以下的没有测试过,我这个应用的需求就是要求6.0以上,有兴趣的同学可以去测试下)。
但是这样设置后,用上一篇的适配键盘上推或是虚拟按键的显示和隐藏适配就不行了,需要重新设置。
增加一个util类:
public class AndroidBug5497Workaround {
// For more information, see https://code.google.com/p/android/issues/detail?id=5497
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
public static void assistActivity (final Activity activity) {
new AndroidBug5497Workaround(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private AndroidBug5497Workaround(final Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
possiblyResizeChildOfContent(activity);
}
});
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
}
private void possiblyResizeChildOfContent(Activity activity) {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int resourceId = activity.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
//获取NavigationBar的高度
int navigateHeight = activity.getResources().getDimensionPixelSize(resourceId);
boolean hasSoftNavigateBar = navigationBarExist2(activity);
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard/4) || hasSoftNavigateBar) {
if (heightDifference == 0){
heightDifference = navigateHeight;
}
// keyboard probably just became visible
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
} else {
// keyboard probably just became hidden
frameLayoutParams.height = usableHeightSansKeyboard;
}
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return r.bottom;
}
/**
* 判断是否虚拟按键,这个方法最管用
* @param activity
* @return
*/
private boolean navigationBarExist2(Activity activity) {
WindowManager windowManager = activity.getWindowManager();
Display d = windowManager.getDefaultDisplay();
DisplayMetrics realDisplayMetrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
d.getRealMetrics(realDisplayMetrics);
}
int realHeight = realDisplayMetrics.heightPixels;
int realWidth = realDisplayMetrics.widthPixels;
DisplayMetrics displayMetrics = new DisplayMetrics();
d.getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels;
int displayWidth = displayMetrics.widthPixels;
return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
}
}
在activity里onCreate里调用:
AndroidBug5497Workaround.assistActivity(this);
OK,这两篇是我在开发过程中遇到的问题,记录并分享。
android 透明状态栏方法及其适配键盘上推(二)的更多相关文章
- android 透明状态栏方法及其适配键盘上推(一)
android的状态栏(statusBar)版本的差异化比较大.在android 4.4 以上和5.x可以设置状态栏背景颜色,但是不可以设置状态栏中字和图标的颜色.而系统默认的statusbar的字体 ...
- 关于如何实现Android透明状态栏的总结
开门见山. 原来做的效果,如下图(顶部有一条明显的橙色状态栏): a1.gif 改过之后(顶部状态栏是透明的): p2.gif 我发现网上写的一些文章,不够简洁明了,我整理了一下,复制粘贴一下 ...
- android开发(50) Android透明状态栏。适用于 4.4 以上及 5.0以上设备
概述 有时候我们想在 andorid 手机上实现一种 跨越 顶部状态栏的效果,比如一张图片直接显示在 状态栏内.比如下图: 这个页面里有张图片,这个图片显示在整个页面的上部分.状态栏是 漂浮在这个图片 ...
- Android 透明状态栏&着色状态栏
Android 5.0 及以上实现方式(android在5.0之后引入Material Design 实现方式相对简单) 透明状态栏,背景浸入状态栏 if (Build.VERSION.SDK_INT ...
- Android 透明状态栏
在 android 4 系统中可以设置透明状态栏. 但在 android 5.0 以上遇到问题.但问题是可以解决的,需要正确的设置 theme. 但是需要注意一点,5以上可以修改 status bar ...
- Android设置透明状态栏和透明导航栏
Android透明状态栏只有在4.4之后有. 在代码中加入下面几行代码即可实现
- 68.Android之透明状态栏
转载:http://www.jianshu.com/p/2f17d0e7f6b0 Android开发中需要透明状态栏,注意:本文只适配Android4.4以上及5.0以上设备 概述 有时候我们想在 a ...
- Android沉浸式状态栏(透明状态栏)最佳实现
Android沉浸式状态栏(透明状态栏)最佳实现 在Android4.4之前,我们的应用没法改变手机的状态栏颜色,当我们打开应用时,会出现上图中左侧的画面,在屏幕的顶部有一条黑色的状态栏,和应用的风格 ...
- Android Immersive Mode (沉浸模式) 还是 Translucent Bars (透明状态栏)
Immersive Mode (沉浸模式) 还是 Translucent Bars (透明状态栏) [科普]什么叫真正的“沉浸式”状态栏? 为什么在国内会有很多用户把「透明栏」(Translucent ...
随机推荐
- CSS 文件的4种引入方式
(1)链接式 : 在网页的<head></head>标签对中使用<link>标记来引入外部样式表文件,使用html规则引入外部css (用得比较多) : < ...
- gulp源码解析(二)—— vinyl-fs
在上一篇文章我们对 Stream 的特性及其接口进行了介绍,gulp 之所以在性能上好于 grunt,主要是因为有了 Stream 助力来做数据的传输和处理. 那么我们不难猜想出,在 gulp 的任务 ...
- [JQuery]JQuery选择器引擎Sizzle
写代码过程中,发现使用JQuery选择器时,$('div.tooltip')和$('.tooltip')的结果不一样,怀疑和选择器的代码逻辑有关(事后证明是代码的低级错误,但是从查找原因的过程中,学到 ...
- 双显卡笔记本安装CUDA+theano、tensorflow环境
原文出处:http://www.cnblogs.com/jacklu/p/6377820.html 个人知乎主页欢迎关注:https://www.zhihu.com/people/jack_lu,相信 ...
- Webappbuilder开发快速预览
Webappbuilder开发快速预览 by 李远祥 Webappbuilder for ArcGIS 是由ArcGIS JavaScripit API和dojo创建的,它允许通过创建自己的widge ...
- ZooKeeper的注意事项
在ZooKeeper中存储的数据是以字节数组的形式存储的,当用Java程序处理数据时要注意. Ephemeral znodes并不会有child znode 只有parent-znode存在,才能创建 ...
- c# 读取app.config遇到生成X.config.config问题
string exePath = System.IO.Path.Combine(Environment.CurrentDirectory, "WindowsFormsApp.exe" ...
- Oracle 生成一张测试表并插入随机数据
--生成随机表 --CREATE table scott.One_Million as ( SELECT ROWNUM AS T_ID, TRUNC(DBMS_RANDOM.VALUE(, )) 年龄 ...
- PostgreSQL指南
PostgreSQL指南 历史简介 最近几年Postgres的关注度变得越来越高. 它加快了Postgres的发展步伐, 与此同时其他 的关系数据库系统的发展放缓. 在数据库领域中 Postgre S ...
- 线上问题debug过程(cat,grep,tr,awk,sort,uniq,comm等工具的综合使用)
问题:发现线上到货单的数量,小于实际到货的数量. 怀疑一些隐藏的条件,将部分唯一码进行了过滤,导致数量变少. 开展了如下的跟踪流程: 1.找到其中一个明细的唯一码 grep 6180e-4b09f p ...