你的位置:首页 > 软件开发 > 操作系统 > 实现Android K的伪沉浸式

实现Android K的伪沉浸式

发布时间:2016-07-22 17:00:07
在Android 5.0之后引入了MD风格,从而状态栏沉浸也成为了一种设计习惯。而停留在之Android L之前的Android系统则不能直接实现沉浸式,这里就介绍一下如何实现Android K系列的伪沉浸式。关于沉浸式效果,这里随便贴几张图吧               A ...

实现Android K的伪沉浸式

在Android 5.0之后引入了MD风格,从而状态栏沉浸也成为了一种设计习惯。而停留在之Android L之前的Android系统则不能直接实现沉浸式,这里就介绍一下如何实现Android K系列的伪沉浸式。

关于沉浸式效果,这里随便贴几张图吧

 

实现Android K的伪沉浸式

              Android L效果图

 

实现Android K的伪沉浸式

               Android 4.2效果图

 

可以看出在Android K系列中,状态栏是渐变的效果

下面开始讲解如何实现上图的效果,在此就不赘述Android L或以上的沉浸式的效果实现了,在

 

题外话:这篇文章介绍的都是使用Android Studio作为IDE开发Android的,如果使用的是其他IDE应按照相关设置进行设置或者配置

 

开始学习伪沉浸式

所谓的其他知识,就是创建一个其他App中的Activity共有的基类BaseActivity,让其继承AppCompatActivity,然后在基类中实现伪沉浸式,这样Acticity就能专注于自己的内容,而把这些共有的设置交给BaseActivity处理。

现在看看这部分如何处理,先看代码:

 1 public class BaseActivity extends AppCompatActivity { 2  3   private int mColor; 4    5   public BaseActivity(){ 6   } 7  8   public BaseActivity(int color){ 9     super();10     mColor = color;11   }12 13   @Override14   protected void onCreate(@Nullable Bundle savedInstanceState) {15     super.onCreate(savedInstanceState );16     StatusBarCompat.compat(this, ContextCompat.getColor(this, mColor));17   }18 19   public static class StatusBarCompat{20 21     private static final int INVALID_VAL = -1;22     private static final int COLOR_DEFAULT = Color.parseColor("#20000000");23 24     public static void compat(Activity activity, int statusColor){25       if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){26         if(statusColor != INVALID_VAL){27           activity.getWindow().setStatusBarColor(statusColor);28         }29         return;30       }31 32       if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT33           && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){34         int color = COLOR_DEFAULT;35         ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);36 37         if(statusColor != INVALID_VAL){38           color = statusColor;39         }40 41         View statusBarView = new View(activity);42         ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,43             getStatusBarHeight(activity));44         statusBarView.setBackgroundColor(color);45 46         contentView.addView(statusBarView, lp);47         //contentView.addView(statusBarView, 0, lp);48       }49     }50 51     public static void compat(Activity activity){52       compat(activity, INVALID_VAL);53     }54 55     public static int getStatusBarHeight(Context context){56       int result = 0;57 58       int resourceId = context.getResources().getIdentifier("status_bar_height",59           "dimen", "android");60 61       if(resourceId > 0){62         result = context.getResources().getDimensionPixelSize(resourceId);63       }64 65       return result;66     }67   }68 }

原标题:实现Android K的伪沉浸式

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录