安卓沉侵式状态栏以及监听ScrollView滑动

-类似于手机QQ空间最顶部的效果

  • 首先是布局,整个屏幕是一个大的framelayout,最上层就是那个状态栏。类似于这种结构,保证下方的状态栏始终浮在最上方:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>
    <FrameLayout>
        <ScrollView>
        </ScrollView>
        <LinearLayout 状态栏>
        </LinearLayout>
    </FrameLayout>
</RelativeLayout>
  • 在onCreate中,判断版本号,如果大于5.0,就将状态栏透明,(如果只是将状态栏和你app的主颜色一致的话,需要在你的activity的xml中加入另外的代码:(android:fitsSystemWindows=”true”,android:clipToPadding=”false”),如果只是将你的状态栏的背景覆盖到手机的状态栏的话只需要如下两行代码:
// 一定要加在setContentView之前
if(isHigherThenKitkat()) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

protected boolean isHigherThenKitkat() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
           return true;
        }
        return false;
    }
  • (如果只是将状态栏和你app的主颜色一致的话,需要在你的activity的xml中加入另外的代码:(android:fitsSystemWindows=”true”,android:clipToPadding=”false”)如果你加入了此行代码,就会发现只会与你的APP的主颜色(加在最顶部的控件中(不是layout中),就会与顶部的颜色)一致,5.0可以自行设置状态栏颜色。
  • 如果没加(android:fitsSystemWindows=”true”,android:clipToPadding=”false”),注意整个页面会向上移一个状态栏的距离,要记得在activity中,判断一下,然后做相应的处理。
//onCreate方法结构:
if(isHigherThenKitkat()) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        }

        setContentView(R.layout.activity_test);
        super.onCreate(savedInstanceState);

        if (isHigherThenKitkat()) {

            //todo 获取手机屏幕的状态栏高度,并重新绘制控件的margin,

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) (56 * getDensity()), (int) (56 * getDensity()));

            lp.setMargins(0, (int) (60*getDensity() + getStatusBarHeight(getApplicationContext())), 0, 0);
            lp.gravity = Gravity.CENTER_HORIZONTAL;

            imageView.setLayoutParams(lp);

        }

/**
     * 用于获取状态栏的高度。 使用Resource对象获取(推荐这种方式)
     *
     * @return 返回状态栏高度的像素值。
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen",
                "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

监听ScrollView的滑动

重写一个ScrollView类,调用接口的方法,主类实现该接口,回调函数中做相应的处理

public class ObservableScrollView extends ScrollView {

    private ScrollViewListener scrollViewListener = null;

    public ObservableScrollView(Context context) {
        super(context);
    }

    public ObservableScrollView(Context context, AttributeSet attrs,
                                int defStyle) {
        super(context, attrs, defStyle);
    }

    public ObservableScrollView(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);
        }
    }


    public void removeScrollViewListener() {
        this.scrollViewListener = null;
    }

}
activity onStop方法: personalScrollView.removeScrollViewListener();
onResumn方法:
personalScrollView.setScrollViewListener(this);
public interface ScrollViewListener {

    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
}
// 在activity中xml文件中的ScrollView记得替换成自定义的ScrollView
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值