android Activity 布局 和 控件属性

一、简介

1、android五大布局:LinearLayout 、TableLayout、RelativeLayout、FrameLayout、AbsoluteLayout

2、

LinearLayout以你为它设置的垂直或水平的属性值,来排列所有的子元素。所有的子元素都被堆放在其它元素之后,因此一个垂直列表的每一行只会有一个元素,而不管他们有多宽,而一个水平列表将会只有一个行高(高度为最高子元素的高度加上边框高度)。LinearLayout保持子元素之间的间隔以及互相对齐(相对一个元素的右对齐、中间对齐或者左对齐)。 
LinearLayout还支持为单独的子元素指定weight 。好处就是允许子元素可以填充屏幕上的剩余空间。这也避免了在一个大屏幕中,一串小对象挤成一堆的情况,而是允许他们放大填充空白。子元素指定一个weight 值,剩余的空间就会按这些子元素指定的weight 比例分配给这些子元素。默认的 weight 值为0。例如,如果有三个文本框,其中两个指定了weight 值为1,那么,这两个文本框将等比例地放大,并填满剩余的空间,而第三个文本框不会放大。

3、

TableLayout 将子元素的位置分配到行或列中。一个TableLayout 由许多的TableRow 组成,每个TableRow 都会定义一个 row (事实上,你可以定义其它的子对象,这在下面会解释到)。TableLayout 容器不会显示row 、cloumns 或cell 的边框线。每个 row 拥有0个或多个的cell ;每个cell 拥有一个View 对象。表格由列和行组成许多的单元格。表格允许单元格为空。单元格不能跨列,这与HTML 中的不一样。

4、

RelativeLayout 允许子元素指定他们相对于其它元素或父元素的位置(通过ID 指定)。因此,你可以以右对齐,或上下,或置于屏幕中央的形式来排列两个元素。元素按顺序排列,因此如果第一个元素在屏幕的中央,那么相对于这个元素的其它元素将以屏幕中央的相对位置来排列。如果使用XML 来指定这个 layout ,在你定义它之前,被关联的元素必须定义。

5、

FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。

6、

AbsoluteLayout 可以让子元素指定准确的x/y坐标值,并显示在屏幕上。(0, 0)为左上角,当向下或向右移动时,坐标值将变大。AbsoluteLayout 没有页边框,允许元素之间互相重叠(尽管不推荐)。我们通常不推荐使用 AbsoluteLayout ,除非你有正当理由要使用它,因为它使界面代码太过刚性,以至于在不同的设备上可能不能很好地工作。

参考:http://dev.10086.cn/cmdn/wiki/index.php?doc-view-3633.html讲的很清楚。

二、LinearLayout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <!--
         android:id   为控件指定相应的ID
         android:text  指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串
         android:grivity  指定控件的基本位置,比如说居中,居右等位置
         android:textSize  指定控件当中字体的大小
         android:background  指定该控件所使用的背景色,RGB命名法 
         android:width  指定控件的宽度
         android:height  指定控件的高度
         android:padding*  指定控件的内边距,也就是说控件当中的内容
         android:sigleLine  如果设置为真的话,则将控件的内容在同一行当中进行显示
     -->
    <TextView
         android:id="@+id/firstText"
         android:text="第一行"
         android:gravity="center_vertical"
         android:textSize="35pt"
         android:background="#aa0000"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:paddingLeft="10dip"
         android:paddingTop="20dip"
         android:paddingRight="30dip"
         android:paddingBottom="40dip"
         android:layout_weight="1"
        android:singleLine="true"/>
    <TextView
         android:id="@+id/secondText"
         android:text="第二行"
         android:gravity="center_vertical"
         android:textSize="15pt"
         android:background="#0000aa"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_weight="1"/>
</LinearLayout>

 

三、TableLayout

android:stretchColumns=”0”         拉伸第0列以充满父控件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="0">
    <!-- TableRow指一行 -->

    <TableRow>
        <TextView
            android:text="@string/row1_column1"
            android:background="#aa0000"
            android:padding="3dip" />
        <TextView
             android:text="@string/row1_column1"
             android:padding="3dip"
             android:gravity="center_horizontal"
            android:background="#00aa00"
             ></TextView>
        <TextView
            android:text="@string/row1_column2"
            android:gravity="right"
            android:background="#0000aa"
            android:padding="3dip" />
    </TableRow>
 
    <TableRow>
        <TextView
            android:text="@string/row2_column1"
            android:padding="3dip" />
        <TextView
            android:text="@string/row2_column2"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>
</TableLayout>

四、RelativeLayout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

<?xml version="1.0" encoding="utf-8"?>
    <!--
        android:layout_above 将该控件的底部至于给定ID的控件之上
        android:layout_below 将该控件的顶部至于给定ID的控件之下
        android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐
        android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐
 
        android:layout_alignBaseline 该控件的baseline和给定ID的控件的baseline对齐
        android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘
        android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐
        android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐
        android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐
 
 
        android:alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐
        android:layout_alignParentLeft 如果该值为true,则将该控件的左边与父控件的左边对齐
        android:layout_alignParentRight 如果该值为true,则将该控件的右边与父控件的右边对齐
        android:layout_alignParentTop 如果该值为true,则将空间的顶部与父控件的顶部对齐
 
        android:layout_centerHorizontal 如果值为真,该控件将被至于水平方向的中央
        android:layout_centerInParent 如果值为真,该控件将被至于父控件水平方向和垂直方向的中央
        android:layout_centerVertical 如果值为真,该控件将被至于垂直方向的中央
    -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="10px" >
 
    <TextView android:id="@+id/label" 
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:text="Type here:" />
 
    <EditText android:id="@+id/entry" 
              android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:background="@android:drawable/editbox_background"
              android:layout_below="@id/label" />
 
    <Button android:id="@+id/ok" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_below="@id/entry"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="10px"
            android:text="OK" />
 
    <Button android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/ok"
            android:layout_alignTop="@id/ok"
            android:text="Cancel" />
</RelativeLayout>

 

五、RelativeLayout用到的一些重要属性

第一类:属性值为true或false 
    android:layout_centerHrizontal  水平居中 
     android:layout_centerVertical   垂直居中 
    android:layout_centerInparent    相对于父元素完全居中 
    android:layout_alignParentBottom 贴紧父元素的下边缘 
    android:layout_alignParentLeft   贴紧父元素的左边缘 
    android:layout_alignParentRight  贴紧父元素的右边缘 
    android:layout_alignParentTop    贴紧父元素的上边缘 
    android:layout_alignWithParentIfMissing  如果对应的兄弟元素找不到的话就以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name” 
    android:layout_below      在某元素的下方 
    android:layout_above      在某元素的的上方 
    android:layout_toLeftOf   在某元素的左边 
    android:layout_toRightOf  在某元素的右边

    android:layout_alignTop   本元素的上边缘和某元素的的上边缘对齐 
    android:layout_alignLeft  本元素的左边缘和某元素的的左边缘对齐 
    android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐 
    android:layout_alignRight  本元素的右边缘和某元素的的右边缘对齐

第三类:属性值为具体的像素值,如30dip,40px 
    android:layout_marginBottom              离某元素底边缘的距离 
    android:layout_marginLeft                   离某元素左边缘的距离 
    android:layout_marginRight                 离某元素右边缘的距离 
    android:layout_marginTop                   离某元素上边缘的距离

六、控件样式常用属性

<!--
android:id —— 为控件指定相应的ID
android:text —— 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串
android:grivity —— 指定控件的基本位置,比如说居中,居右等位置
android:textSize —— 指定控件当中字体的大小
android:background —— 指定该控件所使用的背景色,RGB命名法 
android:width —— 指定控件的宽度
android:height —— 指定控件的高度
android:padding* —— 指定控件的内边距,也就是说控件当中的内容
android:sigleLine —— 如果设置为真的话,则将控件的内容在同一行当中进行显示
--> 
              
654 321 456 123

七、关于单位dip px,推荐用dip

参考:http://zhidao.baidu.com/question/216026222.html&__bd_tkn__=2ba945133533de264c57b57b93a463a599149de18078338d51fed8133ea5c69d362ad36bb4bcda3b39bb3949f6bbe47087ac3af56e60b1f4e7eb60157a5dfb329f61abfa5c0f03de0125277dd13abd0a3d77ef777a2bbe8dd738457b0529472acc627c303cbcadab9a7e8aaccbdc8d0ccb342af446aa

dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。

好了,入正题吧,先说说px,px就是像素,如果用px,就会用实际像素画,比个如吧,用画一条长度为240px的横线,在480宽的模拟器上看就是一半的屏宽,而在320宽的模拟器上看就是2/3的屏宽了。

而dip,就是把屏幕的高分成480分,宽分成320分。比如你做一条160dip的横线,无论你在320还480的模拟器上,都是一半屏的长度。

所以你的问题就解开了:
问题1:
为什么代码中,我写的 单位px和dip在同一模拟器里面的表示相同?不是说dip!=px的么?

答:如果你的模拟器的宽是320个像素,100dip和 100px是一样的。
问题2:
为什么模拟器1和模拟器2的运行结果 长度不一致?好像模拟器1的长度更长一点(因为模拟器1把512的显示全了)
答:你可能两个模拟器的宽高不一样,所以dip显示的部分就不一样了。
原文地址:https://www.cnblogs.com/helloweworld/p/2675677.html