关于Android沉浸式状态栏的一个做法
2016-10-20 14:44
176 查看
话不多说,直接上代码
首先在build.gradle文件中引入库:
这是布局文件:
MyScrollView是重新写的一个ScrollView,其实也就是给它添加一个滑动事件
接下来就是如何实现沉浸式了。4.4以上版本才可以实现沉浸式,分为4.4到5.0和5.0以上两个版本。
代码就是这些了。虽然实现了效果,不过有个局限性问题,就是布局文件的根元素必须是<RelativeLayout></RelativeLayout>。
首先在build.gradle文件中引入库:
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
这是布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.title.MyScrollViewandroid:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="vertical"> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="300dp" android:orientation="vertical" android:background="@mipmap/qqq"/> <LinearLayout android:layout_width="match_parent" android:layout_height="300dp" android:orientation="vertical" android:background="#999999"/> <LinearLayout android:layout_width="match_parent" android:layout_height="300dp" android:orientation="vertical" android:background="#FF0000"/> <LinearLayout android:layout_width="match_parent" android:layout_height="300dp" android:orientation="vertical" android:background="#FF8000"/> </LinearLayout> </com.title.MyScrollView> <RelativeLayout android:id="@+id/rl2" android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android: 14411 id="@+id/rl" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" android:textSize="18sp" android:textColor="#FFFFFF" android:layout_centerVertical="true" android:layout_marginLeft="10dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="主界面" android:textSize="18sp" android:textColor="#FFFFFF" android:layout_centerInParent="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加" android:textSize="18sp" android:textColor="#FFFFFF" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp"/> </RelativeLayout> </RelativeLayout> </RelativeLayout>
MyScrollView是重新写的一个ScrollView,其实也就是给它添加一个滑动事件
public class MyScrollView extends ScrollView{ public interface ScrollViewListener { void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy); } private ScrollViewListener scrollViewListener = null; public MyScrollView(Context context) { super(context); } public MyScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public void setScrollViewListener(ScrollViewListener scrollViewListener) { this.scrollViewListener = scrollViewListener; } @Override protected void onScrollChanged(int x, int y, int oldx, int oldy) { super.onScrollChanged(x, y, oldx, oldy); if (scrollViewListener != null) { scrollViewListener.onScrollChanged(this, x, y, oldx, oldy); } } }
接下来就是如何实现沉浸式了。4.4以上版本才可以实现沉浸式,分为4.4到5.0和5.0以上两个版本。
private void setView(){ //定义一个LayoutParams RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.height = 100; if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ //设置导航栏总体高度 RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layout.height = 100 + getStatusBarHeight(); rl2.setLayoutParams(layout); layoutParams.setMargins(0, getStatusBarHeight(), 0, 0); Window window = getWindow(); ViewGroup mContentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT); //Android5.0以上 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ //设置透明状态栏,这样才能让 ContentView 向上 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); //设置状态栏颜色 window.setStatusBarColor(Color.TRANSPARENT); View mChildView = mContentView.getChildAt(0); if (mChildView != null) { //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 使其不为系统 View 预留空间. ViewCompat.setFitsSystemWindows(mChildView, false); } }else{ //Android4.4到5.0之间 //首先使 ChildView 不预留空间 View mChildView = mContentView.getChildAt(0); if (mChildView != null) { ViewCompat.setFitsSystemWindows(mChildView, false); } int statusBarHeight = getStatusBarHeight(); //需要设置这个 flag 才能设置状态栏 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //避免多次调用该方法时,多次移除了 View if (mChildView != null && mChildView.getLayoutParams() != null && mChildView.getLayoutParams().height == statusBarHeight) { //移除假的 View. mContentView.removeView(mChildView); mChildView = mContentView.getChildAt(0); } if (mChildView != null) { FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams(); //清除 ChildView 的 marginTop 属性 if (lp != null && lp.topMargin >= statusBarHeight) { lp.topMargin -= statusBarHeight; mChildView.setLayoutParams(lp); } } } } rl.setLayoutParams(layoutParams); } /** * 获取状态栏高度 * @return */ private int getStatusBarHeight() { int result = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; } private void initListeners() { // 获取顶部图片高度后,设置滚动监听 ViewTreeObserver vto = img.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { img.getViewTreeObserver().removeGlobalOnLayoutListener( this); height = img.getHeight(); scrollView.setScrollViewListener(MainActivity.this); } }); } //scrollview的滚动监听 @Override public void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy) { if (y <= 0) { rl2.setBackgroundColor(Color.argb((int) 0, 51, 143, 255)); } else if (y > 0 && y <= height) { float scale = (float) y / height; float alpha = (255 * scale); rl2.setBackgroundColor(Color.argb((int) alpha, 51, 143, 255)); } else { rl2.setBackgroundColor(Color.argb((int) 255, 51, 143, 255)); } }
代码就是这些了。虽然实现了效果,不过有个局限性问题,就是布局文件的根元素必须是<RelativeLayout></RelativeLayout>。
相关文章推荐
- 关于Android沉浸式状态栏字体颜色和图片背景自适应变化
- Android关于全屏设置和隐藏状态栏、沉浸式状态栏的总结
- Android中关于沉浸式状态栏的实现
- Android关于沉浸式状态栏总结
- 关于Android沉浸式通知栏的一个开源库SystemBarTint简单使用
- TitleLayout——一个Android轻松实现通用、标准、支持沉浸式状态栏的标题栏库
- TitleLayout——一个Android轻松实现通用、标准、支持沉浸式状态栏的标题栏库
- Android沉浸式通知栏的一个开源库SystemBarTint简单使用,android沉浸式状态栏
- 关于Android沉浸式状态栏的解决方法之一(伪沉浸式)
- 关于Android沉浸式通知栏的一个开源库SystemBarTint简单使用
- 关于android中使用很多大图片的一些做法(转)
- 转贴一个关于Junit测试Android开发的
- 翻译一个关于Android游戏开发的系列文章---1-The Game Idea
- android 如何在状态栏上增加一个icon
- 【Android Developer】1.一个关于Android机器人的故事
- 设计一个Android关于24点的小游戏之一(24点算法设计)
- Android学习札记36:一个关于onSaveInstanceState ()方法的例子
- 关于下载xbmc后打开Android 源码时的一个思考问题?兼各平台安装xbmc 的中文显示
- 【Android Developer】1.一个关于Android机器人的故事
- Android 标题栏,状态栏放置一个ProgressBar