Android layout secrets
Preface today, I will write down some understanding of the layout. The main content includes the inheritance relationship between the control attributes, the relationship between the control attributes and the container attributes, and the use of various types of attributes. In general, when assigning values to properties of a control, we generally have one type of property, a property starting with layout, the following describes an attribute that does not start with layout _. Copy the Code 1 <RelativeLayout 2 android: layout_width = "match_parent" 3 android: layout_height = "match_parent"> 4 5 <TextView 6 android: layout_width = "wrap_content" 7 android: layout_height = "wrap_content" 8 android: text = "hello_world"/> 9 10 </RelativeLayout> copy the code. I set three attributes for TextView: layout_width, layout_height, and te. Xt. You can see that the layout_width and layout_height attributes of the three attributes start with layout _, while the text attributes do not start with Layout. Attributes starting with layout _ are inherited from the container. In this example, they are inherited from RelativeLayout, and TextView itself does not. Text is the property of TextView. To illustrate that the layout attribute is a container attribute, I used the following example to place the TextView in RelativeLayout and LinearLayout, and set the layout_centerInParent attribute for the TextView, this attribute is owned by RelativeLayout but not by LinearLayout. The experiment code is as follows: Copy code 1 <RelativeLayout 2 android: layout_width = "wrap_content" 3 android: layout_height = "wrap_content"> 4 5 <TextView 6 android: layout_width = "wrap_content" 7 android: layout_height = "wrap_content" 8 android: layout_centerInParent = "true" 9 Droid: text = "hello_world"/> 10 </RelativeLayout> 11 12 <LinearLayout13 android: layout_width = "wrap_content" 14 android: layout_height = "wrap_content"> 15 16 <TextView17 android: layout_width = "wrap_content" 18 android: layout_height = "wrap_content" 19 android: layout_centerInParent = "true" 20 android: text = "hello world2"/> 21 </LinearLayout> after copying the code above, you will find that the editor prompts "Invalid layout param in a LinearLayout: layout_cen "TerInParent", as shown below, this experiment can draw a conclusion that the attribute starting with layout is not owned by TextView, but is inherited from the layout attribute in the container. In this way, we can draw a conclusion, controls can be divided into their own attributes and the layout attributes in the container. Next we will analyze the attributes in detail through TextView and each layout container. The LinearLayout and TextView sections mainly describe the attributes of LinearLayout and TextView. First, let's take a look at the attributes of TextView and the property inheritance relationship. below is the attributes of TextView, XML AttributesAttribute Nameandroid: autoLinkandroid: autoTextandroid: bufferTypeandroid: capitalizeandroid: cursorVisibleandroid: digitsandroid: drawableBottomandroid: drawableEndandroid: Role: drawableStartandroid: drawableTopandroid: editableandroid: Role: ellipsizeandroid: emsandroid: Role: freezesTextandroid: gravityandroid: heightandroid: hintandroid: imeActionIdandroid: Role: inputMethodandroid: inputTypeandroid: Role: linesandroid: Role: maxEmsandroid: maxHeightandroid: maxLeng Thandroid: maxLinesandroid: maxWidthandroid: minEmsandroid: Example: minLinesandroid: minWidthandroid: numericandroid: passwordandroid: Example: shadowColorandroid: shadowDxandroid: shadowDyandroid: Example: textandroid: Example: textAppearanceandroid: textColorandroid: textColorHi Ghlightandroid: textColorHintandroid: textColorLinkandroid: textIsSelectableandroid: textScaleXandroid: textSizeandroid: textStyleandroid: typefaceandroid: width TextView Inherited attribute Inherited XML AttributesFrom class android. view. view Attribute Nameandroid: accessibilityLiveRegionandroid: alphaandroid: backgroundandroid: clickableandroid: contentDescriptionandroid: drawingCacheQualityandroid: duplicateParentStateandroi D: Keys: focusableandroid: Keys: idandroid: Keys: keepScreenOnandroid: layerTypeandroid: layoutdireandroid Android: longClickableandroid: minHeightandroid: minWidthandroid: nextFocusDownandroid: Keys Ndroid: Role: onClickandroid: paddingandroid: Role: paddingStartandroid: paddingTopandroid: Role: rotationandroid: rotationXandroid: Role: scaleXandroid: scaleYandroid: scrollXandroid: scrollYandroid: scrollbarAlwaysDrawHorizontalTra Ckandroid: rule: scrollbarSizeandroid: scrollbarStyleandroid: rule: scrollbarTrackVerticalandroid: scrollbarsandroid: rule: tagandroid: textAlignmentandroid: textDirectionandroid: transformPivotXan Droid: transformPivotYandroid: translationXandroid: translationYandroid: visibility. That is to say, these two parts are all attributes of TextView. However, when editing code in the XML editor, we will find that, in addition to the above attributes, there are many attributes that can be set starting with layout. These are the container attributes. The following uses LinearLayout as an example. The experiment scenario is as follows: copy the Code 1 <LinearLayout 2 android: layout_width = "wrap_content" 3 android: layout_height = "wrap_content"> 4 5 <TextView 6 android: layout_width = "wrap_content" 7 android: layout_height = "wrap_content" 9 android: text =" Hello world2 "/> 10 </LinearLayout> copy the code. Let's take a look at the layout attributes of LinearLayout. the LayoutParams class is displayed in the help document. Its Attributes are XML AttributesAttribute Nameandroid: layout_gravityandroid: layout_weight and inherit from the following attributes: android. view. viewGroup. marginLayoutParams Attribute Nameandroid: layout_marginBottomandroid: layout_marginEndandroid: layout_marginLeftandroid: layout_marginRightandroid: layout_marginStartandroid: layout_m ArginTop and android. view. viewGroup. layoutParams Attribute Name Related Method Descriptionandroid: layout_height Specifies the basic height of the view. android: layout_width Specifies the basic width of the view. all the preceding attributes are the layout Attributes provided by the layout control. Therefore, the attributes that TextView can set are its own attributes and the preceding layout attributes. Note: We analyzed the layout attribute usage in Android through the combination of LinearLayout and TextView. Through the above analysis, we can understand the origin of the control attribute, in this way, you can make the interface layout as you like. Similarly, you can analyze RelativeLayout, TableLayout, and so on.