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;}


更多相关文章

  1. Android 软键盘弹出 【遮挡输入框】【遮挡RecyclerView】的问题
  2. android编辑布局文件时非要降低api level或者更改主题才能显示布
  3. Android 线性布局:LinearLayout
  4. Android学习笔记-Android非布局activity中布局文件及控件加载方
  5. Android应用开发学习之相对布局
  6. Android merge抽象布局介绍
  7. android View页面布局总结 最全总结。
  8. Android系统回顾(三):UI之GridLayout布局

随机推荐

  1. 用Eclipse开发和调试Android应用程序(二)
  2. 迈向Android的第一步 - 搭建Android开发
  3. android-----EditText
  4. android的PreferenceActivity
  5. Weekly Metasploit Update: Android WebV
  6. android XML总结
  7. android图片等比例缩放 填充屏幕
  8. android 使用xml制作背景图片(shape的用法
  9. android之AutoCompleteTextView控件用法
  10. Android对话框的高级设置《二》设置对话