android沉浸式+虚拟按键+Fragment+CoordinatorLayout(2)

android沉浸式+CoordinatorLayout+Fragment

CoordinatorLayout和沉浸式还是上一篇一样的需求要求图片要深入到标题栏,如果不是这样的问题可以跳过了。

 问题:4.0手机和5.0不兼容问题,4.0手机是可以在标题栏上显示了但是Toolbar会遮挡标题栏,但是5.0以上的完全不可以。

问题解决方案

coordinator= (CoordinatorLayout) findViewById(R.id.coordinator);

coordinator.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if(coordinator.getWidth()>0) {//判断时候难道正确高度而不是0
                coordinator.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    CollapsingToolbarLayout.LayoutParams laout = new CollapsingToolbarLayout.LayoutParams(goodsImageIv.getWidth(), goodsImageIv.getHeight());
                    laout.setMargins(0, 0 - Util.getBarHeight(context), 0, 0);
                    goodsImageIv.setLayoutParams(laout);
                                    }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    CollapsingToolbarLayout.LayoutParams laout = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();
                    laout.setMargins(0, 0 + Util.getBarHeight(context), 0, 0);
                    toolbar.setLayoutParams(laout);
                }
            }
        }
    }
});
第一个监听主要是当显示后能够拿到像素。一定要在获取到高度之后加上coordinator.getViewTreeObserver().removeOnGlobalLayoutListener(this);代码取消监听,不让一直在刷新。

//5.0以上操作的goodsimageiv是我要沉浸到标题栏上的图片或者其他对象如果还有其他对象,如果发现有在CoordinatorLayout的对象和xml上设置的下边距不对的话也要在设置,父类布局.setLayoutParams(laout);可以用同一个laout

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                CollapsingToolbarLayout.LayoutParams laout = new                         CollapsingToolbarLayout.LayoutParams(goodsImageIv.getWidth(), goodsImageIv.getHeight());
                laout.setMargins(0, 0 - Util.getBarHeight(context), 0, 0);
                 goodsImageIv.setLayoutParams(laout);

    父类布局.setLayoutParams(laout);//边距不对或者其他要设置向上移动距离的
    }

4.0上的操作只需要把toolbar设置一下就ok了,主要问题就是toolbar遮挡了。

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    CollapsingToolbarLayout.LayoutParams laout = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();
                    laout.setMargins(0, 0 + Util.getBarHeight(context), 0, 0);
                    toolbar.setLayoutParams(laout);
   }

问题:最下面的控件会被遮挡一部分

问题解决方案就在被遮挡的布局下面在加一个布局撑起来比较投机哈哈。


问题:4.0的手机中间某个控件会有一个慕名奇妙的白条或者其他颜色的条

这个问题出现在RecyclerView上,莫名的距离上边的空间有一个边距,尝试设置控件之间距离,失败,然后设置内间距解决了。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    mRecyclerView.setPadding(0,0,0,0);
}

问题:如果在Fragment+CoordinatorLayout中使用沉浸下方会出现一条白条或其他颜色的条状物。

解决:醉醉的了以为和上面以上的呢,发现设置之后不对,发现问题是CoordinatorLayout获取的大小是不算标题栏的,但是绘画区域是在标题栏开始绘画的,但是高是没有加上标题栏。所以会出现白条或其他颜色的条状物。

main_content.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if(main_content.getWidth()>0) {
                main_content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                main_content.setLayoutParams(new FrameLayout.LayoutParams(main_content.getWidth(), main_content.getHeight() +Util.getBarHeight(context)));
               
               
            }
        }
    }
});
main_content是我的 CoordinatorLayout


getBarHeight方法代码,求状态栏的高度

public static int getBarHeight(Context context){
    Class<?> c = null;
    Object obj = null;
    Field field = null;
    int x = 0, sbar = 38;//默认为38,貌似大部分是这样的

    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        sbar = context.getResources().getDimensionPixelSize(x);

    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return sbar;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值