1.一些常用的公共属性介绍
1) layout_width -宽
fill_parent: 宽度和父元素相同,wrap_content: 宽度随本身的内容所调整,或者指定 px值来设置宽
2) layout_height - 高
fill_parent: 高度和父元素相同,wrap_content: 高度随本身的内容所调整,或者指定 px值来设置高
3) background -设置背景图
4) padding -设置边距
可以具体设置paddingBottom,paddingLeft,paddingRight,paddingTop来设定不同的px值
5) id -该object的id号
@+id/id1 代表添加新的id名为id1, @id/id1 代表引用id1的控件
6) layout_weight -重要度
个人理解为显示的优先级。默认为0(最高),数值越大,优先级越低!参考下面的Linear Layout例子。要让layout_weight生效,需要父层或父父层的相应layout_width/layout_height = "fill_parent",否则wrap_content会压缩到最小足够空间!
7) layout_gravity- Container组件的对齐方式
组件在layout里面的对齐方式。
8) gravity-文字在组件里的对齐方式
例如设置button里面的文字在button中居中显示。
* 大多数属性是可以调用对应的函数来动态改变状态的,请查看SDK Doc。
2. Linear Layout 线形布局
orientation -容器内元素的排列方式。vertical: 子元素们垂直排列,horizontal: 子元素们水平排列。在代码里可通过setOrientation()进行动态改变,值分别为HORIZONTAL或者VERTICAL。
*在Linear Layout, 宽度/高度都是按着组件的次序逐个占用的!所以当某个组件设置"fill_parent",在没有设置Layout_weight的情况下,该组件会占用了余下的空间,那么在它后面的组件就会显示不出来。如下图的EditText如果没有设置android:layout_weight="1",它下面的其他组件就看不见了!
baselineAligned 一般情况下,这个属性默认为true,代表在同一方向的组件都基于第一个组件对齐。所以可以看到下图的text1, button1, text2是在同一水平线的。当不需要这效果时,可以设置为false。
xml代码:
view sourceprint?01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
03 android:orientation="vertical"
04 android:layout_width="fill_parent"
05 android:layout_height="fill_parent" >
06 <TextView
07 android:text="@string/hello"
08 android:layout_width="fill_parent"
09 android:layout_height="wrap_content"
10 />
11 <EditText
12 android:layout_width="fill_parent"
13 android:layout_height="wrap_content"
14 android:layout_weight="1"
15 android:id="@+id/edittext"
16 />
17 <LinearLayout
18 android:id="@+id/LinearLayout01"
19 android:layout_width="fill_parent"
20 android:layout_height="wrap_content"
21 android:orientation="horizontal">
22 <TextView
23 android:text="text1"
24 android:id="@+id/TextView01"
25 android:layout_width="wrap_content"
26 &