Textview如何一左一右
时间: 2024-04-10 16:35:11 浏览: 21
要实现 TextView 左右对齐的效果,可以使用 LinearLayout 或 RelativeLayout 这样的布局容器来包裹两个 TextView,并分别设置它们的对齐方式。
以下是一种使用 LinearLayout 实现左右对齐的示例:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Left Aligned Text"
android:gravity="left" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Right Aligned Text"
android:gravity="right" />
</LinearLayout>
```
在这个示例中,我们使用了一个水平方向的 LinearLayout,并在其中放置了两个 TextView。通过将 layout_weight 设置为相等的值,两个 TextView 将平均占据父容器的宽度。然后,通过设置每个 TextView 的 gravity 属性为 "left" 和 "right",分别实现左对齐和右对齐的效果。
你也可以使用 RelativeLayout 或其他适合你需求的布局容器来实现类似的效果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)