对齐TableLayout中的文本单元格



如何对齐两列TableLayout中的文本内容,使左列中的文本向右对齐,左列中的文字向左对齐,使它们在屏幕中间相互"固定"?

它应该看起来像这样:

+-----------------------------------+
|           Title: | Value          |
+-----------------------------------+

这里有一个小例子,提供了您想要的

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="2">
        <TextView
            android:text="left at right"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:paddingRight="5dp" />
        <TextView
            android:text="right at left"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="left"
            android:paddingLeft="5dp" />
    </TableRow>
</TableLayout>

假设您使用的是TextView

使用属性

<TextView
  ....
  android:gravity="center|right"` 
/>

<TextView
  ....
  android:gravity="center|left"
/>

在xml布局中

java 中的textView.setGravity(Gravity.RIGHT|Gravity.CENTER);textView.setGravity(Gravity.LEFT|Gravity.CENTER);

最新更新