現在有3個按鈕,如何讓這3個按鈕以水平方向分別左對齊、居中對齊和右對齊。
使用FrameLayout和android:layout_gravity屬性可以很容易實現這個布局。
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>
怎樣實現5個按鈕成梅花狀排列,并整體水平居中。

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
如何重用布局文件?
可以使用<include>標簽引用其他的布局文件,并用android:id屬性覆蓋被引用布局文件中頂層節點的android:id屬性值。代碼如下:
<!--引用mylayout.xml文件-->
<include android:id="@+id/layout1" layout="@layout/mylayout">
android:layout_gravity屬性和android:gravity有什么區別?
android:layout_gravity屬性指定了當前View在父View中的位置,而android:gravity屬性指定了View中內容(文本、圖像或其他View)的位置。
android:padding屬性和android:layout_margin屬性的區別是什么?
android:padding屬性用于設置View中的內容距View邊緣的距離,而android:layout_margin屬性用于設置View距離其他View或父容器邊緣的距離。
請寫出顯示一個Toast信息框的Java代碼。
Toast toast = Toast.makeText(this,"我的信息", Toast.LENGTH_LONG);
toast.show();