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 ...
随机推荐
- 与中国最顶尖sharepoint工程师共舞
最近又跳了,来到某家外企.自以为善能称心如意,谁知乃井里之蛙. 给我的最大感触是,做sharepoint一定要做过非常大型的部署开发,没有经过这种淬炼,天天闷声研究,做一些页面功能,对技术提升毫无帮助 ...
- CodeForces 460B
Little Dima and Equation Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- 微信小程序之快速接入七牛云
小程序为什么要接入云? 目前,开发者在开发小程序过程中,主要遇到以下几个问题: 小程序发布大小超限 微信官方限制小程序的发布代码不能超过 1MB,而在实际开发过程中,一般的小程序难免会有图片等富媒体文 ...
- Disruptor深入解读
将系统性能优化到极致,永远是程序爱好者所努力的一个方向.在java并发领域,也有很多的实践与创新,小到乐观锁.CAS,大到netty线程模型.纤程Quasar.kilim等.Disruptor是一个轻 ...
- C# 6 与 .NET Core 1.0 高级编程 - 38 章 实体框架核心(上)
译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 38 章 实体框架核心(上)),不对的地方欢迎指出与交流. 章节出自<Professional C# 6 ...
- 关于自定义的 XIB cell上的 button如何在控制器里实现点击方法
直接调用cell.button addTarget 的方法点击事件是失效的 这时需要你在xib中设置button的tag值 然后在返回cell的时候添加点击事件 UIButton *button = ...
- Android EventBus 3.0 实例使用详解
EventBus的使用和原理在网上有很多的博客了,其中泓洋大哥和启舰写的非常非常棒,我也是跟着他们的博客学会的EventBus,因为是第一次接触并使用EventBus,所以我写的更多是如何使用,源码解 ...
- phpcms 列表项 内容项
根据上一篇内容继续 首页替换完成后 接下来替换列表页 首先把列表的静态网页放入相应模板的content文件夹下,并改名为 list.html 并且创建栏目时选择下面一项 同样,头尾去掉,利用{temp ...
- 配置IIS Express以便通过IP地址访问调试的网站
问题背景 最近使用C#编写了一个WebService,希望通过Java进行调用.使用Visual Studio 2013调试WebService时,可以在浏览器中通过localhost地址访问WSDL ...
- MJRefresh在UITableView中的使用
前言 MJRefresh是一个好用的上下拉刷新的控件,github地址如下:https://github.com/CoderMJLee/MJRefresh 很多app都使用这个控件,我们也来了解一下它 ...