【问题标题】:How to align text to the right [duplicate]如何将文本向右对齐[重复]
【发布时间】:2013-01-16 09:13:10
【问题描述】:

我试图弄清楚如何将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

【问题讨论】:

    标签: android


    【解决方案1】:

    您已经接近了,请为您的 TextView 尝试常规的“gravity”而不是“layout_gravity”。

    【讨论】:

    • 不幸的是,这不起作用:-\
    【解决方案2】:

    最后一个 TextView 中的 layout_width="wrap_content" 取消了 gravity="right"。将其更改为fill_parentmatch_parent,它应该可以工作。

    【讨论】:

      【解决方案3】:

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

      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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-09
        • 2018-11-30
        • 1970-01-01
        • 1970-01-01
        • 2020-01-03
        • 2018-03-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多