Android学习——LinearLayout布局实现居中、左对齐、右对齐

android:orientation="vertical"表示该布局下的元素垂直排列;

在整体垂直排列的基础上想要实现内部水平排列,则在整体LinearLayout布局下再创建一个LinearLayout布局。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:id="@+id/activity_main"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent">
 7 
 8 
 9     <TextView
10         android:id="@+id/textView"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:text="合计"
14         android:textSize="20sp"
15         android:gravity="center"
16         />
17 
18     <TextView
19         android:id="@+id/textView2"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:text="1619元"
23         android:textSize="20sp"
24         android:gravity="center"
25         android:layout_marginTop="23dp"
26         />
27 
28     <LinearLayout
29         android:layout_width="fill_parent"        //在这个LinearLayout下,不指定orientation就默认horizontal
30         android:layout_height="wrap_content"
31         android:layout_marginTop="23dp"
32         >
33         <TextView
34             android:layout_width="wrap_content"
35             android:layout_height="wrap_content"
36             android:text="总里程:"
37             />
38 
39         <TextView
40             android:layout_width="match_parent"
41             android:layout_height="wrap_content"
42             android:gravity="right"
43             android:text="266.86公里"
44             android:id="@+id/textView3"
45             />
46     </LinearLayout>
47 
48     <LinearLayout
49         android:layout_width="fill_parent"
50         android:layout_height="wrap_content"
51         android:layout_marginTop="23dp">
52         <TextView
53             android:layout_width="wrap_content"
54             android:layout_height="wrap_content"
55             android:text="运费:"/>
56         <TextView
57             android:layout_width="match_parent"
58             android:layout_height="wrap_content"
59             android:gravity="right"
60             android:textColor="@color/colorAccent"
61             android:text="1639元"
62             />
63     </LinearLayout>
64 
65     <LinearLayout
66         android:layout_width="fill_parent"
67         android:layout_height="wrap_content"
68         android:layout_marginTop="23dp">
69         <TextView
70             android:layout_width="wrap_content"
71             android:layout_height="wrap_content"
72             android:text="起步价(含5公里):"/>
73         <TextView
74             android:layout_width="match_parent"
75             android:layout_height="wrap_content"
76             android:gravity="right"
77             android:text="1571元"
78             />
79     </LinearLayout>
80 
81 </LinearLayout>

实现效果如下:

posted @ 2017-07-13 22:27  最咸的鱼  阅读(43700)  评论(0编辑  收藏  举报