TextView的长文推了,尽管ellipsize =结束网格布局等意见网格、推了、布局、结束

2023-09-06 00:08:45 作者:东方不败Ⅻ

我的问题是非常相似的How得到的布局,其中一个文本可以成长,ellipsize,而不是狼吞虎咽的布局中的其他元素,但建议有阅读下面为什么我不能用TableLayouts。

我试图创建一个列表视图行,基本上是这样的:

| TextView中|查看1 |查看2 |

所有视图包含可变宽度的元素。 TextView的有ellipsize =结束集。查看1应对准TextView的左侧,同时查看2应对准屏幕的右侧。因此,通常情况下,会有空白视图1和View 2作为TextView的文字越来越长,TextView的要长,推视图1到右边,直到不再有空格左边之间。然后,ellipsize要踢,切割TextView的文本,并附加一个省略号(...)结尾。

所以,结果应该是这样的:

  + ------------------------------------- --- +
|简短的文字[厂景] [视图2] |
+ ---------------------------------------- +
|长文本ELL ... [厂景] [视图2] |
+ ---------------------------------------- +
 

我已经试过:

TableLayouts,但他们似乎使滚动某些设备上极其缓慢。 RelativeLayouts,但我要么有重叠的观点,或厂景或视图2完全消失。 GridLayouts,但TextView的总增长,直到它占用了屏幕的整个宽度,从而推动厂景和视图2出的画面。

这是我试过的网格布局:

 <网格布局
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT>

    <的TextView
        机器人:layout_gravity =左| fill_horizo​​ntal
        机器人:ellipsize =结束
        机器人:单线=真
        机器人:文本=长文本来证明问题的TextView在网格布局占用太多的空间,尽管省略号/>

    <的TextView
        机器人:layout_gravity =左
        机器人:文本=(视图1)/>

    <的TextView
        机器人:layout_gravity =右
        机器人:文本=(视图2)/>
< /网格布局>
 
Android TextView 显示字数的限制问题

查看1和View 2是不是真的TextViews,我只是用他们的例子,以简化的东西。

有没有办法实现这一目标而无需使用TableLayouts?

编辑: 按照要求,这里是我尝试用RelativeLayout的解决这一点。 TextView的占用在这种情况下,屏幕的整个宽度,所以既不厂景也不视图2可见。

 < RelativeLayout的
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT>

    <的TextView
        机器人:ID =@ + ID / RL0
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:ellipsize =结束
        机器人:单线=真
        机器人:文本=长文本来证明问题的TextView在网格布局占用太多的空间,尽管省略号/>

    <的TextView
        机器人:ID =@ + ID / RL1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_toRightOf =@ ID / RL0
        机器人:layout_marginLeft =10dp
        机器人:文本=(视图1)/>

    <的TextView
        机器人:ID =@ + ID / RL2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_toRightOf =@ ID / RL1
        机器人:layout_alignParentRight =真
        机器人:layout_marginLeft =10dp
        机器人:文本=(视图2)/>
< / RelativeLayout的>
 

解决方案

我LinearLayouts的大风扇,所以这里是使用这些我的建议:

 <的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向>

    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=横向
        机器人:layout_weight =1>

        <的TextView
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:ellipsize =结束
            机器人:单线=真
            机器人:文本=长文本来证明问题的TextView在网格布局占用太多的空间,尽管省略号/>

        <的TextView
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=(视图1)/>
    < / LinearLayout中>

    <的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =右
        机器人:文本=(视图2)/>
< / LinearLayout中>
 

My problem is very similar to How to get a layout where one text can grow and ellipsize, but not gobble up the other elements on the layout, but read on below why I can't use TableLayouts as proposed there.

I'm trying to create a listview row that basically looks like this:

| TextView | View 1 | View 2 |

All views contain variable width elements. The TextView has ellipsize="end" set. View 1 should align left of the TextView, while View 2 should align to the right of the screen. So, normally, there would be whitespace between View 1 and View 2. As the text in the TextView grows longer, the TextView should grow, pushing View 1 to the right until there is no more whitespace left. Then, ellipsize should kick in, cutting of the text in TextView and appending an ellipsis ("...") at the end.

So, the result should look something like this:

+----------------------------------------+
| short text [view1]             [view2] |
+----------------------------------------+
| long text with ell ... [view1] [view2] |
+----------------------------------------+

I've tried:

TableLayouts, but they seem to make scrolling extremely slow on some devices. RelativeLayouts, but I either had overlapping views, or view1 or view2 disappeared completely. GridLayouts, but the TextView always grows until it takes up the whole width of the screen, thus pushing view1 and view2 out of the screen.

This is the GridLayout I tried:

<GridLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_gravity="left|fill_horizontal"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="Long text to demonstrate problem with TextView in GridLayout taking up too much space despite ellipsis" />

    <TextView
        android:layout_gravity="left"
        android:text="(view1)" />

    <TextView
        android:layout_gravity="right"
        android:text="(view2)" />
</GridLayout>

View 1 and View 2 are not really TextViews, I just used them in the example to simplify things.

Is there any way to achieve this without using TableLayouts?

EDIT: As requested, here is my attempt at solving this with a RelativeLayout. The TextView takes up the full width of the screen in this case, so neither view1 nor view2 are visible.

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/rl0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="Long text to demonstrate problem with TextView in GridLayout taking up too much space despite ellipsis" />

    <TextView
        android:id="@+id/rl1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/rl0"
        android:layout_marginLeft="10dp"
        android:text="(view1)" />

    <TextView
        android:id="@+id/rl2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/rl1"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dp"
        android:text="(view2)" />
</RelativeLayout>

解决方案

I'm a big fan of LinearLayouts, so here's my suggestion using those:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="Long text to demonstrate problem with TextView in GridLayout taking up too much space despite ellipsis" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="(view1)" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="(view2)" />
</LinearLayout>