【问题标题】:Align parts of text in a TextView在 TextView 中对齐部分文本
【发布时间】:2015-11-14 08:45:13
【问题描述】:
我正在开发一个接收数据并应该以特定方式显示这些数据的 Android 应用程序。我收到三个字符串(例如 A、B 和 C),我必须以这种方式将它们附加到我自己创建的 TextView 中:
|A B C|
A 必须左对齐,B 居中,C 右对齐。
我真的不知道该怎么做; TextView 的尺寸可以改变取决于用于运行应用程序的屏幕大小,但是这些字符串必须在我在每种可能的情况下显示的位置上。
感谢您的帮助!
Ps:这些字符串可以用粗体显示吗???
【问题讨论】:
标签:
java
android
text
textview
alignment
【解决方案1】:
1) 请在linearlayout中使用3xTextView,方向水平
2) 线性布局android:width="match_parent"
3) 在文本视图中设置 android:layout_gravity 为 A) left B) center C) right
4) 为TextView设置android:layout_weight=1
文本视图的粗体:
android:textStyle="bold"
【解决方案2】:
试试这个....
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="Text1"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Text2"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Text3"
android:textStyle="bold" />
</LinearLayout>