【问题标题】:Why don't everybody use ViewGroup.LayoutParams instead of LinearLayout.LayoutParams?为什么不是每个人都使用 ViewGroup.LayoutParams 而不是 LinearLayout.LayoutParams?
【发布时间】:2016-01-09 14:28:57
【问题描述】:

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

看来LinearLayout.LayoutParams 不是从ViewGroup.LayoutParams 继承的函数。

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

【问题讨论】:

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


    【解决方案1】:

    LinearLayout需要使用LinearLayout.LayoutParams,RelativeLayout需要使用RelativeLayout.LayoutParams,否则会崩溃。虽然MATCH_PARENT 和WRAP_CONTENT 的常量相同,但特定的布局参数会向布局参数添加附加信息,例如在RelativeLayout 中,RelativeLayout.LayoutParams 存储您指定为centerInParent 或@ 的规则987654328@ 或toRightOf 等。如果你给它一个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;
    

    【讨论】:

      【解决方案2】:

      那么有什么理由我们应该使用 LinearLayout.LayoutParams 而不是另一个

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

      【讨论】:

      • 好的,我明白了,我因为这条线而感到困惑,new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);,没有注意到 ViewGroup 的东西只适用于常量
      猜你喜欢
      • 1970-01-01
      • 2016-10-27
      • 2010-12-17
      • 2010-11-19
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      相关资源
      最近更新 更多