Search code examples
androidandroid-layoutandroid-linearlayoutandroid-relativelayout

Why don't everybody use ViewGroup.LayoutParams instead of LinearLayout.LayoutParams?


Because it can be used either with Relative or Linear layouts. So it could be useful in case we later change from RelativeLayout to LinearLayout.

And it seems LinearLayout.LayoutParams is not an inherited function from ViewGroup.LayoutParams.

So is there any reason we should use LinearLayout.LayoutParams over the other one? Is there any layout type specific advantages in using this LinearLayout.LayoutParams over the common thing?


Solution

  • You need to use LinearLayout.LayoutParams for LinearLayout, and you need to use RelativeLayout.LayoutParams for RelativeLayout, otherwise you will crash. While the constants for MATCH_PARENT and WRAP_CONTENT are the same, the specific layout params add additional information to the layout params, for example in RelativeLayout, the RelativeLayout.LayoutParams stores the rules that you specify as centerInParent or below or toRightOf, etc. If you give it a LinearLayout.LayoutParams, it'll just crash.

    For example, this is LinearLayout.LayoutParams

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

    And this is RelativeLayout.LayoutParams

    public static class LayoutParams extends ViewGroup.MarginLayoutParams {
            @ViewDebug.ExportedProperty(category = "layout", resolveId = true, indexMapping = {
                @ViewDebug.IntToString(from = ABOVE,               to = "above"),
                @ViewDebug.IntToString(from = ALIGN_BASELINE,      to = "alignBaseline"),
                @ViewDebug.IntToString(from = ALIGN_BOTTOM,        to = "alignBottom"),
                @ViewDebug.IntToString(from = ALIGN_LEFT,          to = "alignLeft"),
                @ViewDebug.IntToString(from = ALIGN_PARENT_BOTTOM, to = "alignParentBottom"),
                @ViewDebug.IntToString(from = ALIGN_PARENT_LEFT,   to = "alignParentLeft"),
                @ViewDebug.IntToString(from = ALIGN_PARENT_RIGHT,  to = "alignParentRight"),
                @ViewDebug.IntToString(from = ALIGN_PARENT_TOP,    to = "alignParentTop"),
                @ViewDebug.IntToString(from = ALIGN_RIGHT,         to = "alignRight"),
                @ViewDebug.IntToString(from = ALIGN_TOP,           to = "alignTop"),
                @ViewDebug.IntToString(from = BELOW,               to = "below"),
                @ViewDebug.IntToString(from = CENTER_HORIZONTAL,   to = "centerHorizontal"),
                @ViewDebug.IntToString(from = CENTER_IN_PARENT,    to = "center"),
                @ViewDebug.IntToString(from = CENTER_VERTICAL,     to = "centerVertical"),
                @ViewDebug.IntToString(from = LEFT_OF,             to = "leftOf"),
                @ViewDebug.IntToString(from = RIGHT_OF,            to = "rightOf"),
                @ViewDebug.IntToString(from = ALIGN_START,         to = "alignStart"),
                @ViewDebug.IntToString(from = ALIGN_END,           to = "alignEnd"),
                @ViewDebug.IntToString(from = ALIGN_PARENT_START,  to = "alignParentStart"),
                @ViewDebug.IntToString(from = ALIGN_PARENT_END,    to = "alignParentEnd"),
                @ViewDebug.IntToString(from = START_OF,            to = "startOf"),
                @ViewDebug.IntToString(from = END_OF,              to = "endOf")
            }, mapping = {
                @ViewDebug.IntToString(from = TRUE, to = "true"),
                @ViewDebug.IntToString(from = 0,    to = "false/NO_ID")
            })
    
            private int[] mRules = new int[VERB_COUNT];
            private int[] mInitialRules = new int[VERB_COUNT];
    
            private int mLeft, mTop, mRight, mBottom;
    
            private boolean mRulesChanged = false;
            private boolean mIsRtlCompatibilityMode = false;
    
            /**
             * When true, uses the parent as the anchor if the anchor doesn't exist or if
             * the anchor's visibility is GONE.
             */
            @ViewDebug.ExportedProperty(category = "layout")
            public boolean alignWithParent;