Android布局揭秘
前言
今天把对于布局的一些理解写下来,主要内容包括控件的属性的继承关系,控件与容器的属性的关系,以及各种类的属性的使用。
控件的属性种类
通常意义上讲,我们在对一个控件进行属性赋值的时候大体上有种类型的属性,一种为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
这一小节主要介绍下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继承属性
http://www.cnblogs.com/luoaz/p/3947100.html