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

关于Android沉浸式状态栏的一个做法

2016-10-20 14:44 176 查看
话不多说,直接上代码

首先在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>。

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