不知道大家在编写布局的时候有没有这样的疑问:在 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