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 @   最咸的鱼  阅读(44244)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
点击右上角即可分享
微信分享提示