您的位置:首页 > 移动开发 > Android开发

Android, 四种基本布局

2015-09-15 19:45 573 查看
1.LinearLayout

android:orientation="vertical"垂直线性布局,"horizontal"水平线性布局

android:gravity="top"(buttom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal)控制布局中控件的对齐方式。如果是没有子控件的控件设置此属性,表示其内容的对齐方式,比如说TextView里面文字的对齐方式;若是有子控件的控件设置此属性,则表示其子控件的对齐方式,gravity如果需要设置多个属性值,需要使用“|”进行组合

android:gravity 与 android:layout_gravity的区别

android:gravity是指定本元素的子元素相对它的对齐方式。

android:layout_gravity是指定本元素相对它的父元素的对齐方式。

android:layout_weight="1"通过设置控件的layout_weight属性以控制各个控件在布局中的相对大小,线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1,那么这两个按钮都会被拉伸到整个屏幕宽度的一半。如果layout_weight指为0,控件会按原大小显示,不会被拉伸;对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度,再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度。

例:



布局代码:

1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2     xmlns:tools="http://schemas.android.com/tools"
3     android:layout_width="match_parent"
4     android:layout_height="match_parent"
5     android:orientation="vertical"
6     tools:context=".LinearLayoutActivity" >
7
8     <LinearLayout
9         android:layout_width="match_parent"
10         android:layout_height="match_parent"
11         android:layout_weight="1"
12         android:orientation="horizontal" >
13
14         <Button
15             android:layout_width="wrap_content"
16             android:layout_height="match_parent"
17             android:layout_weight="1"
18             android:background="#aa0000"
19             android:gravity="center_horizontal|center_vertical"
20             android:text="第一列"
21             android:textSize="15sp" >
22         </Button>
23
24         <Button
25             android:layout_width="wrap_content"
26             android:layout_height="match_parent"
27             android:layout_weight="1"
28             android:background="#00aa00"
29             android:gravity="center_horizontal"
30             android:text="第二列"
31             android:textSize="15sp" >
32         </Button>
33
34         <Button
35             android:layout_width="wrap_content"
36             android:layout_height="match_parent"
37             android:layout_weight="1"
38             android:background="#0000aa"
39             android:gravity="center|bottom"
40             android:text="第三列"
41             android:textSize="15sp" >
42         </Button>
43
44         <Button
45             android:layout_width="wrap_content"
46             android:layout_height="match_parent"
47             android:layout_weight="1"
48             android:background="#aaaa00"
49             android:gravity="bottom"
50             android:text="第四列"
51             android:textSize="15sp" >
52         </Button>
53     </LinearLayout>
54
55     <LinearLayout
56         android:layout_width="match_parent"
57         android:layout_height="match_parent"
58         android:layout_weight="1"
59         android:orientation="vertical" >
60
61         <Button
62             android:layout_width="match_parent"
63             android:layout_height="match_parent"
64             android:layout_weight="1"
65             android:gravity="bottom"
66             android:text="第1行"
67             android:textSize="15sp" >
68         </Button>
69
70         <Button
71             android:layout_width="match_parent"
72             android:layout_height="match_parent"
73             android:layout_weight="1"
74             android:gravity="bottom"
75             android:text="第2行"
76             android:textSize="15sp" >
77         </Button>
78
79         <Button
80             android:layout_width="match_parent"
81             android:layout_height="match_parent"
82             android:layout_weight="1"
83             android:gravity="bottom"
84             android:text="第3行"
85             android:textSize="15sp" >
86         </Button>
87
88         <Button
89             android:layout_width="match_parent"
90             android:layout_height="match_parent"
91             android:layout_weight="1"
92             android:gravity="bottom"
93             android:text="第4行"
94             android:textSize="15sp" >
95         </Button>
96     </LinearLayout>
97
98 </LinearLayout>

2.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 离某元素上边缘的距离
实例



<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_toLeftOf="@+id/button3"
        android:text="Button 1"
        />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_toRightOf="@+id/button3"
        android:text="Button 2"
        />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button 3"
        />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/button3"
        android:layout_below="@+id/button3"
        android:text="Button 4"
        />
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/button3"
        android:layout_below="@+id/button3"
        android:text="Button 5"
        />

</RelativeLayout></span>

3.FrameLayout

这种布局没有任何定位方式,所有的控件都会摆放在左上角。















<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >        <Button        android:id="@+id

tton1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button"        />    <ImageView        android:id="@+id/image_view"        android:layout_width="wrap_content"    	android:layout_height="wrap_content"    	android:src="@drawable/ic_launcher"    /> </FrameLayout>
4.TableLayout设计表格时,我们尽量应该让每一行都拥有相同的列数,这样的表格才会简单。


<span style="font-size:18px;"><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:stretchColumns="1"
    >
<span style="white-space:pre">	</span><TableRow>
    <TextView
<span style="white-space:pre">		</span>android:layout_height="wrap_content"
<span style="white-space:pre">		</span>android:text="Account:"/>
    <EditText
        android:id="@+id/account"
        android:layout_height="wrap_content"
        android:hint="Input your account"/>
    </TableRow>
    
<span style="white-space:pre">	</span><TableRow>
<span style="white-space:pre">	</span>    <TextView
<span style="white-space:pre">	</span>        android:layout_height="wrap_content"
<span style="white-space:pre">	</span>        android:text="Password:"/>
<span style="white-space:pre">	</span>    <EditText
<span style="white-space:pre">	</span>        android:id="@+id/password"
<span style="white-space:pre">	</span>        android:layout_height="wrap_content"
<span style="white-space:pre">	</span>        android:inputType="textPassword"/>
    </TableRow>
    
<span style="white-space:pre">	</span><TableRow>
<span style="white-space:pre">	</span>    <Button 
<span style="white-space:pre">	</span>        android:id="@+id/login"
<span style="white-space:pre">	</span>        android:layout_height="wrap_content"
<span style="white-space:pre">	</span>        android:layout_span="2"
<span style="white-space:pre">	</span>        android:text="Login"/>
<span style="white-space:pre">	</span>    </TableRow>
    
</TableLayout>
</span>







                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: