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

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

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

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

那么我们有什么理由应该使用 LinearLayout.LayoutParams 而不是另一个?使用此 LinearLayout.LayoutParams 是否有任何布局类型特定的优势?

最佳答案

LinearLayout需要使用LinearLayout.LayoutParams,RelativeLayout需要使用RelativeLayout.LayoutParams,否则会崩溃。虽然 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;

关于android - 为什么不是每个人都使用 ViewGroup.LayoutParams 而不是 LinearLayout.LayoutParams?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34694660/

相关文章:

android - 无法在新线程中启动 Activity

android - 基于更改并存储到 map 中的 DataSnapshot 出错

java - 抽屉导航菜单子(monad)类别

android - 想在单个 TextView 中有多种文本显示样式

Android 线性布局 - 如何将元素保持在 View 底部?

java - 如何从 SQLite 数据库中获取特定月份的数据

android - 垂直 LinearLayout 的分隔线?

android - RTL-Layout 中忽略准则(Android Constraint-Layout)

android - 使用 LinearLayout 的自定义小部件没有得到 onDraw()

android - 动态设置 LinearLayout 的宽度?