【问题标题】:Have two TextViews side by side, the first stretched and limited, the second fixed有两个 TextView 并排,第一个被拉伸和限制,第二个固定
【发布时间】:2011-05-03 13:01:56
【问题描述】:

我一直在尝试获得某种布局,但我所有的尝试都失败了。

我在有限的空间内有 2 个文本视图。只要有空间显示两者,我希望它们一个接一个地显示:

| T1 T2222          |
| T111111 T2222     |

但是当空间不足时,我只希望第一个被椭圆化或以其他方式切割,例如:

| T1111111... T2222 |

这可能有帮助 - 或者没有 - 两者都应该只有 1 行。

固定第二个的宽度可能会有所帮助,也可能没有帮助。

  • 我已经在Two TextViews side by side, only one to ellipsize? 尝试过这些想法,但这种情况与我的不同,因为他们的 T2 是为了锚定在右侧,而我想让 T2 跟随 T1。

  • 我尝试为 T2 指定 minWidth,但似乎没有得到尊重;普通宽度是,但我似乎无法控制 T1 的行为。

  • 我涉足过重量,但没有发现任何可以解决问题的方法。但这可能是我的经验不足。

  • 我可以为 T1 指定一个 maxWidth,很荣幸它可以按照我的意愿行事,但问题是有限空间的整个宽度是未指定的。

此时我开始认为没有办法做到这一点 - 除非可能以编程方式,这是我想避免的路线。

感谢您的任何建议。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    将两个字符串连接起来并将结果放入单个TextViewellipsize="middle" 集合是否有效?

    还是布局,第二个文本视图向右对齐?

    | T1          T2222 |
    | T111111     T2222 |
    | T1111111... T2222 |
    

    更新
    利用第二个文本视图的宽度可以固定的能力,下面的RelativeLayout 提供所需的布局(通过将第二个视图的宽度显式设置为 60dp):

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent" 
        android:gravity="fill">
        <TextView android:id="@+id/secondText" 
            android:layout_width="60dp" android:layout_height="wrap_content"
            android:singleLine="true" android:layout_alignParentRight="true" />
        <TextView android:id="@+id/dynamicText" 
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_marginRight="5dp" android:layout_toLeftOf="@id/secondText"
            android:singleLine="true" android:ellipsize="end"  />   
    </RelativeLayout>
    

    上面的代码生成如下布局:

    | T1 T2222          |
    | T111111 T2222     |
    | T1111111... T2222 |
    

    在两个TextViews 之间留出 5dp 的间隙。

    【讨论】:

    • 1.我没有想到这一点,这是一个有趣的想法,虽然不是设计师的想法。谢谢。 2.不,T2必须在T1旁边。
    • 请看我更新中的布局,希望对你有用。
    • 感谢一百万! :) 我已经放弃了,但你的例子很完美。我会投票赞成答案,但我今天刚刚注册......只是为了记录:没有gravity =“fill”,RelativeLayout只会从右边填充;如果没有首先声明 T2,则 T2 将被省略而不是 T1。
    • 我很高兴,你做到了,更高兴的是,你让我学到的知识不满足于挑战。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多