如何将文本右对齐

时间:2013-01-16 00:23:18

标签: android

我正在试图弄清楚如何将dashboardNewsItemDate对齐,而不是左对齐。目前,它会立即显示在dashboardNewsItemHeadline旁边。我认为把它放在TableRow范围内会使android:layout_gravity="right"函数符合我的意愿,但事实证明这是错误的。

<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_gravity="center">
    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/dashboardNewsItemHeadline" android:layout_gravity="left"
                />
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/dashboardNewsItemDate" android:layout_gravity="right" android:autoText="false"/>
    </TableRow>
</TableLayout>

最后,我的目标是内容,所以这样显示......

My news title                       Mar 3
-----------------------------------------
This is a bit of a longer news      Jul 2
title and it wraps

4 个答案:

答案 0 :(得分:0)

你很近,为你的TextViews尝试常规的“gravity”而不是“layout_gravity”。

答案 1 :(得分:0)

最后一个TextView中的layout_width="wrap_content"正在取消gravity="right"。将其更改为fill_parentmatch_parent,它应该有效。

答案 2 :(得分:0)

我在这里提到另一个问题,找到了我对这个问题的答案。

Percentage width in a RelativeLayout

答案 3 :(得分:0)

有一个神奇的属性可以解决你的问题:

android:stretchColumns="*"

将此属性添加到TableLayout

<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_gravity="center"
        android:stretchColumns="*">
    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/dashboardNewsItemHeadline" android:layout_gravity="left"
                />
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/dashboardNewsItemDate" android:layout_gravity="right" android:autoText="false"/>
    </TableRow>
</TableLayout>
相关问题