ViewGroup 的 generateLayoutParams 方法的作用

不知道大家在编写布局的时候有没有这样的疑问:在 RelativeLayout 布局里面可以对子控件使用 layout_alignParentRight 等属性,但是在 FrameLayout 或者 LinearLayout 中就没有,这是为什么呢?

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" />

    </RelativeLayout>

而且,在代码中,我们获取子控件的 LayoutParams 的时候,默认是 ViewGroup.LayoutParams 类型的,我们想要使用某些特殊的字段,还需要转换为 RelativeLayout.LayoutParams 或其他具体的 LayoutParams 才行。这又是为什么呢?

下面,我们一一道来。

当我们自定义一个控件的时候,如果我们做的灵活一点,一般会提供一些属性配置方法。

    public TagGroup(Context context, AttributeSet attrs) {
   
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TagGroup);

        int targetColorInt = typedArray.getInt(R.styleable.TagGroup_targetColor
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值