两个并排的 TextViews,只有一个用于椭圆化? [英] Two TextViews side by side, only one to ellipsize?

查看:28
本文介绍了两个并排的 TextViews,只有一个用于椭圆化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让两个 TextView 元素并排显示(在列表项中),一个向左对齐,一个向右对齐.类似的东西:

I want to have two TextView elements appear side by side (in a list item), one aligned to the left, one to the right. Something like:

|<TextView>               <TextView>|

(| 代表屏幕的四肢)

但是,左侧的 TextView 可能包含太长而无法适应屏幕的内容.在这种情况下,我想让它椭圆化,但仍然显示整个正确的 TextView.类似的东西:

However, the TextView on the left can have content that is too long to fit on the screen. In this case, I want to have it ellipsize but still show the entire right TextView. Something like:

|This is a lot of conte...<TextView>|

我对此进行了多次尝试,同时使用了 LinearLayoutRelativeLayout,而我想出的唯一解决方案是使用 RelativeLayoutcode> 并在左侧 TextView 上放置一个 marginRight 足够大以清除右侧的 TextView.但是,您可以想象,这并不是最佳选择.

I have had numerous attempts at this, using both LinearLayout and RelativeLayout, and the only solution I have come up with is to use a RelativeLayout and put a marginRight on the left TextView big enough to clear the right TextView. As you can imagine, though, this is not optimal.

还有其他解决方案吗?

最终,LinearLayout 解决方案:

Final, LinearLayout solution:

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    >
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"
        android:inputType="text"
        />
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="0"
        android:layout_gravity="right"
        android:inputType="text"
        />
</LinearLayout>

旧的,TableLayout 解决方案:

Old, TableLayout solution:

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    android:shrinkColumns="0"
    >
    <TableRow>
        <TextView android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true"
            />
        <TextView android:id="@+id/date"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:singleLine="true"
            android:ellipsize="none"
            android:gravity="right"
            />
    </TableRow>
</TableLayout>

推荐答案

使用 TableLayout 并将两个 TextView 放在表格行中,试试看.我没试过

Use TableLayout and put both TextView in table row, have a try. I haven't tried

这篇关于两个并排的 TextViews,只有一个用于椭圆化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆