layoutparser_你知道什么什么吗

layoutparser_你知道什么什么吗从概念讲起LayoutParams,顾名思义,就是布局参数。而且大多数人对此都是司空见惯,我们XML文件里面的每一个View都会接触到layout_xxx这样的属性,这实际上就是对布局参数的描述。大概大家也就清楚了,layout_这样开头的东西都不属于View,而是控制具体显示在哪里。LayoutParams都有哪些初始化方法通常来说,我们都会把我们的控件放在XML文件…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

从概念讲起

LayoutParams,顾名思义,就是布局参数。而且大多数人对此都是司空见惯,我们 XML 文件里面的每一个 View 都会接触到 layout_xxx 这样的属性,这实际上就是对布局参数的描述。大概大家也就清楚了,layout_ 这样开头的东西都不属于 View,而是控制具体显示在哪里。

LayoutParams 都有哪些初始化方法

通常来说,我们都会把我们的控件放在 XML 文件中,即使我们有时候需要对屏幕做比较「取巧」的适配,会直接通过 View.getLayoutParams() 这样的方法获取 LayoutParams 的实例,但我们接触的少并不代表它的初始化方法不重要。

实际上,用代码写出来的 View 加载效率要比在 XML 中加载快上大约 1 倍。只是在如今手机配置都比较高的情况下,我们常常忽略了这种方式。
我们来看看 ViewGroup.LayoutParams 到底有哪些构造方法。

public LayoutParams(Context c, AttributeSet attrs) { 
   
    TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
    setBaseAttributes(a,
            R.styleable.ViewGroup_Layout_layout_width,
            R.styleable.ViewGroup_Layout_layout_height);
    a.recycle();
}

public LayoutParams(int width, int height) { 
   
    this.width = width;
    this.height = height;
}

public LayoutParams(LayoutParams source) { 
   
    this.width = source.width;
    this.height = source.height;
}

LayoutParams() { 
     }
MarginLayoutParams

除去最后一个放给 MarginLayoutParams 做处理的方法外,我们在 ViewGroup 中还有 3 个构造方法。他们分别负责给 XML 处理、直接让用户指定宽高、还有类似集合的 addAll() 这样的方式的赋值方法。

实际上,ViewGroup 的子类的 LayoutParams 类拥有更多的构造方法,感兴趣的自己翻阅源码查看。在这里我想更加强调一下我上面提到的

MarginLayoutParams。

MarginLayoutParams 继承于 ViewGroup.LayoutParams。

public static class MarginLayoutParams extends ViewGroup.LayoutParams { 
   
    @ViewDebug.ExportedProperty(category = "layout")
    public int leftMargin;

    @ViewDebug.ExportedProperty(category = "layout")
    public int topMargin;

    @ViewDebug.ExportedProperty(category = "layout")
    public int rightMargin;

    @ViewDebug.ExportedProperty(category = "layout")
    public int bottomMargin;

    @ViewDebug.ExportedProperty(category = "layout")
    private int startMargin = DEFAULT_MARGIN_RELATIVE;

    @ViewDebug.ExportedProperty(category = "layout")
    private int endMargin = DEFAULT_MARGIN_RELATIVE;

    public MarginLayoutParams(Context c, AttributeSet attrs) { 
   
        super();
        TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout);
        setBaseAttributes(a,
                R.styleable.ViewGroup_MarginLayout_layout_width,
                R.styleable.ViewGroup_MarginLayout_layout_height);

        int margin = a.getDimensionPixelSize(
                com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1);
        if (margin >= 0) { 
   
            leftMargin = margin;
            topMargin = margin;
            rightMargin= margin;
            bottomMargin = margin;
        } else { 
   
            int horizontalMargin = a.getDimensionPixelSize(
                    R.styleable.ViewGroup_MarginLayout_layout_marginHorizontal, -1);
            // ... something
        }
        // ... something
    }
}

一看代码,自然就清楚了,为什么我们以前会发现在 XML 布局里, layout_margin 属性的值会覆盖 layout_marginLeft 与 layout_marginRight 等属性的值。

实际上,事实上,绝大部分容器控件都是直接继承 ViewGroup.MarginLayoutParams 而非 ViewGroup.LayoutParams。所以我们再自定义 LayoutParams 的时候记得继承 ViewGroup.MarginLayoutParams 。

在代码里面使用 LayoutParams

前面介绍了 LayoutParams 的几种构造方法,我们下面以 LinearLayout.LayoutParams 来看看几种简单的使用方式。

val textView1 = TextView(this)
textView1.text = "不指定 LayoutParams"
layout.addView(textView1)

val textView2 = TextView(this)
textView2.text = "手动指定 LayoutParams"
textView2.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
layout.addView(textView2)

val textView3 = TextView(this)
textView3.text = "手动传递 LayoutParams"
textView3.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams(100, 100))
layout.addView(textView3)

我们看看 addView() 都做了什么。

public void addView(View child) { 
   
    addView(child, -1);
}

public void addView(View child, int index) { 
   
    if (child == null) { 
   
        throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
    }
    LayoutParams params = child.getLayoutParams();
    if (params == null) { 
   
        params = generateDefaultLayoutParams();
        if (params == null) { 
   
            throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
        }
    }
    addView(child, index, params);
}

@Override
protected LayoutParams generateDefaultLayoutParams() { 
   
    if (mOrientation == HORIZONTAL) { 
   
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    } else if (mOrientation == VERTICAL) { 
   
        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    }
    return null;
}

public void addView(View child, int index, LayoutParams params) { 
   
    if (DBG) { 
   
        System.out.println(this + " addView");
    }
    if (child == null) { 
   
        throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
    }
    requestLayout();
    invalidate(true);
    addViewInner(child, index, params, false);
}

private void addViewInner(View child, int index, LayoutParams params,
        boolean preventRequestLayout) { 
   

       // ...

    if (!checkLayoutParams(params)) { 
   
        params = generateLayoutParams(params);
    }

      // ...
}

@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { 
   
    return p instanceof LinearLayout.LayoutParams;
}

看起来 ViewGroup 真是煞费苦心,如果我们没有给 View 设置 LayoutParams,则系统会帮我们根据 orientation 设置默认的 LayoutParams。甚至是我们即使在 addView() 之前设置了错误的 LayoutParams 值,系统也会我们帮我们进行纠正。

虽然系统已经做的足够完善,帮我们各种矫正错误,但在 addView() 之后,我们还强行设置错误的 LayoutParams,那还是一定会报 ClassCastException 的。

LayoutParams 很重要,每一名 Android 开发都应该尽力地去掌握,只有弄清楚了系统的编写方式,应对上面类似简书的流式布局才能更好处理。

实际上 Google 出的 FlexboxLayout 已经做的相当完美。
当然如果使用的 RecyclerView,还可以自己写一个 FlowLayoutManager 进行处理。

##读者福利限时分享

Android开发资料+面试架构资料 免费分享 点击链接 即可领取

《Android架构师必备学习资源免费领取(架构视频+面试专题文档+学习笔记)》

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/193546.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 域名url转发怎么设置_url和域名

    域名url转发怎么设置_url和域名URL转发技术就是将该域名的网站访问请求,通过浏览器端技术,转向到另外一个网站。域名URL转发分为url显性转发和URL隐性转发这里以万网域名为例教你如何设置显性url转发和隐性URL转发。

    2022年10月18日
    0
  • 分享一下学习css,js心得

    简化代码,使页面简洁!web前端开发——将界面更好呈现给用户!要了解在不同浏览器上的兼容情况、渲染原理和存在的bug!网站性能优化、SEO;代码的可维护性、性能;网站重构的本质:建立一个前端

    2021年12月22日
    40
  • 防火墙的AAA认证[通俗易懂]

    防火墙的AAA认证[通俗易懂]防火墙的AAA认证防火墙的AAA提供基于三种数据库认证:LOCAL,TACACS+,RADIUS.使用tacacs+,radius两种认证协议来进行专门的AAA服务器与NAS之间进行接入访问认证。基本认证原理:客户机发起验证请求到NAS,而NAS帮助收集用户的验证信息并使用pre-sharekey与AAA服务器进行交互,AAA会对NAS提交上来的…

    2022年6月5日
    69
  • SM4加密运算_数据加密标准des采用的密码类型是

    SM4加密运算_数据加密标准des采用的密码类型是javaSM4加密运算自己的理解:sm4加密,其实就是将字符串转成byte数组,再设置一个与解密相同的字符串,将byte的数据再转成二级制的数据,进行异或的运算,得到一组没有规则的字符串!案例上面是一些算法下面是应用方法这是自己的认知,有什么不同建议可以留言,XXXX是自己加密的字符串…

    2022年10月6日
    1
  • Elasticlunr.js 简单介绍

    Elasticlunr.js 简单介绍

    2022年1月29日
    258
  • 浏览器安装为知笔记插件(chrome浏览器)

    浏览器安装为知笔记插件(chrome浏览器)1.下载并安装驱动 wiz.cn/zh-cn/downloads-webclipper.html2.我选择的谷歌浏览器,因为不好翻墙,这里选择手动安装3.下载会得到一个crx文件。注意这里按照官方文档去添加到扩展程序会失败。我们要把这个文件后缀名改为.zip形式并解压,得到文件夹。注意:是直接将.crx改成.zip!!不是自己压缩成的4.接着,打开谷歌的“更多工具”…

    2022年8月18日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号