3

Потому что он может использоваться либо с Relative, либо Linear макетами. Поэтому это может быть полезно, если мы позже изменим с RelativeLayout на LinearLayout.Почему не все используют ViewGroup.LayoutParams вместо LinearLayout.LayoutParams?

И кажется LinearLayout.LayoutParams не является наследованной функцией от ViewGroup.LayoutParams.

Так есть ли какая-либо причина, по которой мы должны использовать LinearLayout.LayoutParams над другим? Существуют ли какие-либо особенности типа макета при использовании этого LinearLayout.LayoutParams по сравнению с обычным делом?

ответ

2

Вам необходимо использовать LinearLayout.LayoutParams для LinearLayout, и вам необходимо использовать RelativeLayout.LayoutParams для RelativeLayout, иначе вы столкнетесь. В то время как константы MATCH_PARENT и WRAP_CONTENT одинаковы, специфический PARAMS макета добавить дополнительную информацию к Params макета, например, в RelativeLayout, то RelativeLayout.LayoutParams хранит правила, которые определяют, как centerInParent или below или 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, вы столкнетесь во время работы с ClassCastException, когда код LinearLayout пробует лить ViewGroup.LayoutParams в LinearLayout.LayoutParams.

+0

хорошо я понял, я запутался из-за этой линии, 'новые LayoutParams (ViewGroup.LayoutParams .WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); ', не заметил, что объект ViewGroup был только для констант – SadeepDarshana

Смежные вопросы