【问题标题】:How can I display two horizontal textviews, one of which resizes depending on content?如何显示两个水平文本视图,其中一个根据内容调整大小?
【发布时间】:2014-11-05 19:49:09
【问题描述】:

我有一个ListView,它显示了两个TextView,如下所示:

| Each row   |    has the |
| same width | as the row |
| before it  |  like this |

但我想做这样的事情:

| Each left column | uses |
| as much space |   as it |
| needs |       like this |

这可以只用 XML 完成吗?我试过modifying this solution,但没有正常工作。

【问题讨论】:

    标签: android xml resize textview autoresize


    【解决方案1】:

    您应该在RelativeLayout 中包装文本视图。为两者指定android:layout_width="wrap_content",分别为左右TextViews指定android:layout_alignParentLeft="true"android:layout_alignParentRight="true"。也分别设置android:gravity="left"android:gravity="right"

    但在这种情况下,长文本可能会重叠

    另一种选择是使用layout_weight:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="left"
            android:text="some text"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="right"
            android:text="text"/>
    
    </LinearLayout>
    

    【讨论】:

    • 我试过了,左边的文字会和右边的文字重叠。有没有办法确保它们之间有填充?
    • 是的,对于填充尝试LinearLayout 解决方案(见编辑)并将androd:paddingRight="3dp" 添加到左侧TextView,或androd:paddingLeft="3dp" 到右侧,或两者兼而有之
    【解决方案2】:

    类似于下面的东西应该可以工作

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:text="New Text"
                android:id="@+id/textView3" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:text="New Text"
    
                android:layout_gravity="end"
                android:id="@+id/textView4" />
        </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 2017-01-05
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      • 2019-08-27
      • 2016-10-28
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多