标签: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 <LinearLayout 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" > 15 16 <TextView 17 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的属性,先来看下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继承属性
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
标签:des android style blog http color os 使用 io
原文地址:http://www.cnblogs.com/luoaz/p/3947100.html