安卓:TableLayout 第一列靠右对齐,第二列靠左对齐

6
我该怎么做才能得到像这样的东西: 图像示例 在 HTML 中我可以很容易地做到,但在 Android 上怎么做呢?

我无法看到你的图片,因为受到限制,请将你的 XML 复制在这里,这样我就能够理解了。 - Mit Bhatt
3个回答

2
Use TextViews and gravity 

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right"
                android:text="key" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="left"
                android:text="value" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right"
                android:text="More key" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="left"
                android:text="More value" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right"
                android:text="Even more key" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="left"
                android:text="Even more value" />
        </TableRow>
    </TableLayout>

它可以工作,但是还有另一个问题。表格中的单元格应该是相等的宽度(假设为50%),但实际上它们是33%(第一个)和66%(第二个)。 - Dario Rossi
写一个第一个TextView的宽度为1,第二个TextView的宽度为2。 - Venky

1
Write first TextView wight=1 and second TextView wight=2


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right"
                android:text="key" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:gravity="left"
                android:text="value" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right"
                android:text="More key" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:gravity="left"
                android:text="More value" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="right"
                android:text="Even more key" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:gravity="left"
                android:text="Even more value" />
        </TableRow>
    </TableLayout>

</LinearLayout>

0
在您的TableLayout列中,您必须使用TextView来显示内容。
因此,对于第一列,请尝试以下操作,
<TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="Key"
        android:textColor="@android:color/black"/>

对于第二列,请尝试这个:

<TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:text="Value"
        android:textColor="@android:color/black"/>

可以运行,谢谢。但是单元格宽度不相等(它们应该是50%(第一个)和50%(第二个))。 - Dario Rossi
@DarioRossi Venky展示的代码完美无缺。她/他的代码有什么问题吗? - βhargavḯ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接