現(xiàn)在有3個(gè)按鈕,如何讓這3個(gè)按鈕以水平方向分別左對(duì)齊、居中對(duì)齊和右對(duì)齊。
使用FrameLayout和android:layout_gravity屬性可以很容易實(shí)現(xiàn)這個(gè)布局。

1 <?xml version="1.0" encoding="utf-8"?>
2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="horizontal" >
6 <Button
7 android:layout_width="wrap_content"
8 android:layout_height="wrap_content"
9 android:layout_gravity="left"
10 android:text="按鈕1"/>
11 <Button
12 android:layout_width="wrap_content"
13 android:layout_height="wrap_content"
14 android:layout_gravity="center_horizontal"
15 android:text="按鈕1"/>
16 <Button
17 android:layout_width="wrap_content"
18 android:layout_height="wrap_content"
19 android:layout_gravity="right"
20 android:text="按鈕1"/>
21 </FrameLayout>
2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="horizontal" >
6 <Button
7 android:layout_width="wrap_content"
8 android:layout_height="wrap_content"
9 android:layout_gravity="left"
10 android:text="按鈕1"/>
11 <Button
12 android:layout_width="wrap_content"
13 android:layout_height="wrap_content"
14 android:layout_gravity="center_horizontal"
15 android:text="按鈕1"/>
16 <Button
17 android:layout_width="wrap_content"
18 android:layout_height="wrap_content"
19 android:layout_gravity="right"
20 android:text="按鈕1"/>
21 </FrameLayout>
怎樣實(shí)現(xiàn)5個(gè)按鈕成梅花狀排列,并整體水平居中。

1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:layout_gravity="center_horizontal" >
6
7 <!-- 左上角的按鈕 -->
8 <Button
9 android:id="@+id/button1"
10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content"
12 android:text="按鈕1" />
13
14 <!-- 中心的按鈕 -->
15 <Button
16 android:id="@+id/button2"
17 android:layout_width="wrap_content"
18 android:layout_height="wrap_content"
19 android:layout_below="@id/button1"
20 android:layout_toRightOf="@id/button1"
21 android:text="按鈕2" />
22
23 <!-- 左下角的按鈕 -->
24 <Button
25 android:layout_width="wrap_content"
26 android:layout_height="wrap_content"
27 android:layout_toLeftOf="@id/button2"
28 android:layout_below="@id/button2"
29 android:text="按鈕3" />
30
31 <!-- 右上角的按鈕 -->
32 <Button
33 android:layout_width="wrap_content"
34 android:layout_height="wrap_content"
35 android:layout_above="@id/button2"
36 android:layout_toRightOf="@id/button2"
37 android:text="按鈕4" />
38
39 <!-- 右下角的按鈕 -->
40 <Button
41 android:layout_width="wrap_content"
42 android:layout_height="wrap_content"
43 android:layout_toRightOf="@id/button2"
44 android:layout_below="@id/button2"
45 android:text="按鈕5" />
46
47 </RelativeLayout>
48
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:layout_gravity="center_horizontal" >
6
7 <!-- 左上角的按鈕 -->
8 <Button
9 android:id="@+id/button1"
10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content"
12 android:text="按鈕1" />
13
14 <!-- 中心的按鈕 -->
15 <Button
16 android:id="@+id/button2"
17 android:layout_width="wrap_content"
18 android:layout_height="wrap_content"
19 android:layout_below="@id/button1"
20 android:layout_toRightOf="@id/button1"
21 android:text="按鈕2" />
22
23 <!-- 左下角的按鈕 -->
24 <Button
25 android:layout_width="wrap_content"
26 android:layout_height="wrap_content"
27 android:layout_toLeftOf="@id/button2"
28 android:layout_below="@id/button2"
29 android:text="按鈕3" />
30
31 <!-- 右上角的按鈕 -->
32 <Button
33 android:layout_width="wrap_content"
34 android:layout_height="wrap_content"
35 android:layout_above="@id/button2"
36 android:layout_toRightOf="@id/button2"
37 android:text="按鈕4" />
38
39 <!-- 右下角的按鈕 -->
40 <Button
41 android:layout_width="wrap_content"
42 android:layout_height="wrap_content"
43 android:layout_toRightOf="@id/button2"
44 android:layout_below="@id/button2"
45 android:text="按鈕5" />
46
47 </RelativeLayout>
48
如何重用布局文件?
可以使用<include>標(biāo)簽引用其他的布局文件,并用android:id屬性覆蓋被引用布局文件中頂層節(jié)點(diǎn)的android:id屬性值。代碼如下:
<!--引用mylayout.xml文件-->
<include android:id="@+id/layout1" layout="@layout/mylayout">
android:layout_gravity屬性和android:gravity有什么區(qū)別?
android:layout_gravity屬性指定了當(dāng)前View在父View中的位置,而android:gravity屬性指定了View中內(nèi)容(文本、圖像或其他View)的位置。
android:padding屬性和android:layout_margin屬性的區(qū)別是什么?
android:padding屬性用于設(shè)置View中的內(nèi)容距View邊緣的距離,而android:layout_margin屬性用于設(shè)置View距離其他View或父容器邊緣的距離。
請(qǐng)寫出顯示一個(gè)Toast信息框的Java代碼。
Toast toast = Toast.makeText(this,"我的信息", Toast.LENGTH_LONG);
toast.show();