为什么不是每个人都使用ViewGroup.LayoutParams而不是LinearLayout.LayoutParams?

时间:2016-01-09 14:28:58

标签: android android-layout android-linearlayout android-relativelayout

因为它可以与RelativeLinear布局一起使用。因此,如果我们稍后从RelativeLayout更改为LinearLayout,则可能会有用。

似乎LinearLayout.LayoutParams不是来自ViewGroup.LayoutParams的继承函数。

那么我们应该使用LinearLayout.LayoutParams而不是另一个吗?使用这个LinearLayout.LayoutParams比普通的东西有任何布局类型特定的优势吗?

2 个答案:

答案 0 :(得分:2)

  

那么我们是否应该使用LinearLayout.LayoutParams而不是另一个

LinearLayout需要LinearLayout.LayoutParams,正如您可以通过阅读the source code for LinearLayout看到的那样。如果您提供其他内容,例如ViewGroup.LayoutParams,那么当ClassCastException代码尝试将LinearLayout转换为ViewGroup.LayoutParams时,您将在运行时使用LinearLayout.LayoutParams崩溃。

答案 1 :(得分:2)

你需要使用LinearLayout.LayoutParams进行LinearLayout,你需要使用RelativeLayout.LayoutParams进行RelativeLayout,否则你会崩溃。虽然MATCH_PARENTWRAP_CONTENT的常量相同,但特定布局参数会向布局参数添加其他信息,例如在RelativeLayout中,RelativeLayout.LayoutParams存储规则您指定为centerInParentbelowtoRightOf等。如果您给它LinearLayout.LayoutParams,它就会崩溃。

例如,这是LinearLayout.LayoutParams

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

这是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;