標籤:des android style blog http color os 使用 io
前言
今天把對於布局的一些理解寫下來,主要內容包括控制項的屬性的繼承關係,控制項與容器的屬性的關係,以及各種類的屬性的使用。
控制項的屬性種類
通常意義上講,我們在對一個控制項進行屬性賦值的時候大體上有種類型的屬性,一種為layout_開頭的屬性,一種為不是以layout_開頭的屬性,下面以TextView為例進行說明,如下所示
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>
我為TextView設定了三個屬性layout_width、layout_height以及text,可以看到這三個屬性中layout_width、layout_height屬性為layout_開頭而text沒有以Layout_開頭。
以layout_開頭的屬性為從容器中繼承的屬性,在這個例子裡面即是從RelativeLayout中繼承來的,TextView本身並沒有此屬性。而text則是TextView自身擁有的屬性。
為了說明layout屬性為容器屬性,我做了下面的例子,把TextView分別放置到RelativeLayout和LinearLayout中,然後對TextView設定layout_centerInParent屬性,之所有選擇這個屬性,是因為這個屬性為RelativeLayout所有而LinearLayout沒有,實驗代碼如下
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 android: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>
如上進行設定之後會發現,編輯器提示“Invalid layout param in a LinearLayout: layout_centerInParent”,如下所示
通過此實驗可以得出結論,layout開頭的屬性並非TextView所擁有,而是繼承的容器中關於布局的屬性,繼而推而廣之,可以得出結論,控制項的屬性可以分為自身屬性和容器中的布局屬性。下面就通過TextView和各個版面配置容器一起來詳細分析下屬性。
LinearLayout和TextView
這一小節主要介紹下LinearLayout和TextView的屬性,先來看下TextView的屬性,及屬性繼承關係,
下面為TextView自身所擁有的屬性
| XML Attributes |
| Attribute Name |
| android:autoLink |
| android:autoText |
| android:bufferType |
| android:capitalize |
| android:cursorVisible |
| android:digits |
| android:drawableBottom |
| android:drawableEnd |
| android:drawableLeft |
| android:drawablePadding |
| android:drawableRight |
| android:drawableStart |
| android:drawableTop |
| android:editable |
| android:editorExtras |
| android:ellipsize |
| android:ems |
| android:fontFamily |
| android:freezesText |
| android:gravity |
| android:height |
| android:hint |
| android:imeActionId |
| android:imeActionLabel |
| android:imeOptions |
| android:includeFontPadding |
| android:inputMethod |
| android:inputType |
| android:lineSpacingExtra |
| android:lineSpacingMultiplier |
| android:lines |
| android:linksClickable |
| android:marqueeRepeatLimit |
| android:maxEms |
| android:maxHeight |
| android:maxLength |
| android:maxLines |
| android:maxWidth |
| android:minEms |
| android:minHeight |
| android:minLines |
| android:minWidth |
| android:numeric |
| android:password |
| android:phoneNumber |
| android:privateImeOptions |
| android:scrollHorizontally |
| android:selectAllOnFocus |
| android:shadowColor |
| android:shadowDx |
| android:shadowDy |
| android:shadowRadius |
| android:singleLine |
| android:text |
| android:textAllCaps |
| android:textAppearance |
| android:textColor |
| android:textColorHighlight |
| android:textColorHint |
| android:textColorLink |
| android:textIsSelectable |
| android:textScaleX |
| android:textSize |
| android:textStyle |
| android:typeface |
| android:width |
TextView繼承屬性
Inherited XML Attributes
From class android.view.View
| Attribute Name |
| android:accessibilityLiveRegion |
| android:alpha |
| android:background |
| android:clickable |
| android:contentDescription |
| android:drawingCacheQuality |
| android:duplicateParentState |
| android:fadeScrollbars |
| android:fadingEdgeLength |
| android:filterTouchesWhenObscured |
| android:fitsSystemWindows |
| android:focusable |
| android:focusableInTouchMode |
| android:hapticFeedbackEnabled |
| android:id |
| android:importantForAccessibility |
| android:isScrollContainer |
| android:keepScreenOn |
| android:layerType |
| android:layoutDirection |
| android:longClickable |
| android:minHeight |
| android:minWidth |
| android:nextFocusDown |
| android:nextFocusForward |
| android:nextFocusLeft |
| android:nextFocusRight |
| android:nextFocusUp |
| android:onClick |
| android:padding |
| android:paddingBottom |
| android:paddingEnd |
| android:paddingLeft |
| android:paddingRight |
| android:paddingStart |
| android:paddingTop |
| android:requiresFadingEdge |
| android:rotation |
| android:rotationX |
| android:rotationY |
| android:saveEnabled |
| android:scaleX |
| android:scaleY |
| android:scrollX |
| android:scrollY |
| android:scrollbarAlwaysDrawHorizontalTrack |
| android:scrollbarAlwaysDrawVerticalTrack |
| android:scrollbarDefaultDelayBeforeFade |
| android:scrollbarFadeDuration |
| android:scrollbarSize |
| android:scrollbarStyle |
| android:scrollbarThumbHorizontal |
| android:scrollbarThumbVertical |
| android:scrollbarTrackHorizontal |
| android:scrollbarTrackVertical |
| android:scrollbars |
| android:soundEffectsEnabled |
| android:tag |
| android:textAlignment |
| android:textDirection |
| android:transformPivotX |
| android:transformPivotY |
| android:translationX |
| android:translationY |
| android:visibility |
也就是說這兩部分加起來就是TextView的全部屬性,然而我們在XML編輯器中編輯代碼的時候會發現,除了以上屬性外還有很多以layout開頭的屬性可以設定,這些就是容器的屬性,下面以LinearLayout為例,實驗情境如下
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>
下面我們就看下LinearLayout的布局屬性,通過LinearLayout.LayoutParams類來體現
通過協助文檔可以看到,其自身屬性為以下內容
| XML Attributes |
| Attribute Name |
| android:layout_gravity |
| android:layout_weight |
還有繼承了兩類屬性,android.view.ViewGroup.MarginLayoutParams
| Attribute Name |
| android:layout_marginBottom |
| android:layout_marginEnd |
| android:layout_marginLeft |
| android:layout_marginRight |
| android:layout_marginStart |
| android:layout_marginTop |
和android.view.ViewGroup.LayoutParams
| Attribute Name |
Related Method |
Description |
| android:layout_height |
|
Specifies the basic height of the view. |
| android:layout_width |
|
Specifies the basic width of the view. |
以上所有屬性就是布局控制項提供的布局屬性
所以,TextView可以設定的屬性即為自身屬性和以上的布局屬性。
後記
以上我們通過LinearLayout和TextView的組合分析了Android中布局的屬性使用方式,通過以上的分析使得我們能夠瞭解到控制項屬性的由來,這樣就可以更加隨心所欲的進行介面布局了。
同樣的道理,可以去分析RelativeLayout、TableLayout等等。
原文地址:http://www.cnblogs.com/luoaz/p/3947100.html
Android布局揭秘