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 @   最咸的鱼  阅读(44272)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示