Android布局使用layout_weight、layout_width和maxWidth

5

我正在尝试获取一行文本,类似于

foofoofoo - barbarbar

但如果它不能适合一行,则我想用省略号来替换foo和bar。 即我正在尝试缩短它们的长度。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxWidth="0dip"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:singleLine="true"
        android:ellipsize="true"
        android:text="foofoofoofoo" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:text=" - " />
    <TextView
        android:id="@+id/text_2"
        android:layout_width="wrap_content"                         
        android:layout_height="wrap_content"
        android:maxWidth="0dip"
        android:layout_weight="1"
        android:textColor="#ffffff"
        android:singleLine="true"
        android:ellipsize="true"
        android:text="barbarbarbar" />
</LinearLayout>

请耐心等待,这个方法只能部分起作用。
TextViewlayout_width设置为0diplayout_weight设置为1,意味着它们将各占可用空间的50%。
singleLine设置为true,将ellipsize设置为true,意味着如果文本大于容器,则它们将看起来像foofoo...
因此,如果文本较长,则我的结果应该是

foofoo.. - barbar..

这就是我想要的! 所以这个方法有效。 现在我要解决的问题是,如果第一个TextView(id:text_1)的文本小于layout_width="0dip"layout_weight="1"所给出的50%,我希望它可以使用wrap_content。否则它看起来像:

foo 空格 - barbar..

我希望得到的结果是:

foo - barbarbar

这就是为什么我已经更改了layout_width="wrap_content"并添加了android:maxWidth="0dip",但这没有起作用!它似乎忽略了我说的wrap_content,仍然给这个视图50%!
我读到需要添加android:adjustViewBounds="true"才能使maxWidth起作用,但这没有影响。
2个回答

10

这是我所能做到的最好的,试着更改那些字符串,看它是否按照预期工作。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:shrinkColumns="0,2"
    android:stretchColumns="0,2">
    <TableRow>
        <TextView android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:text="foofoofoo"/>
        <TextView android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-"
            android:gravity="center_horizontal"/>
        <TextView android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:text="barbarbarbarbarbarbarbarbarbarbarbar"/>
    </TableRow>
</TableLayout>

2
移除 android:stretchColumns="0,2",它就可以工作了。到目前为止还没问题!:-) 谢谢 - Blundell
1
@Blundell 哇,我是赏金猎人!很高兴它起作用了,谢谢你的奖励。 - bigstones
2
帮助我们双方。我是一个恩人 :-D - Blundell

2
很难确定您的需求,但我认为答案是,除非在代码中动态调整布局,否则无法实现。原因是您要求系统以多种不同的方式工作。
首先,当视图中的文本数量相同时,您希望视图平均共享空间。
其次,当另一个视图中的文本较少时,您希望视图平均共享空间。
还有许多其他情况可以按任意方式处理。然而,我想传达的是,您要求相同的布局以不同的方式处理事情,这就是为什么您不能使用单个布局来实现这一点的原因。

我想基本上说maxWidth是共享空间(因此省略),但通常只是wrap_content。所以我会假设它会使用wrap_content,除非wrap_content的dip值> 50%,然后它会使用maxWidth(即50%)。对吗? - Blundell
除非wrap_content的dip值大于50%,否则它将使用maxWidth。我真的不确定你想说什么。wrap_content不使用dip值。 - user432209
是的,它不是这样的,但maxWidth必须以某种方式与layout_width交互。所以我指的是在后台它必须计算布局的宽度,如果它大于maxWidth则使用maxWidth。所以我是说wrap_content,但如果(在后台)wrap_content大于50%则使用maxWidth。这不是它的工作原理吗? - Blundell
我认为我明白你的意思。问题在于“wrap_content的理论dip值”会根据视图中的文本内容而发生变化。你无法设计一个适用于所有情况的布局,就像你现在尝试做的那样。请参考我的原始答案。 - user432209
我不明白为什么layout_width="wrap_content"和maxWidth="0dip"不能工作 :-(。用英语来说,这意味着“用50%的最大宽度包裹内容”。 - Blundell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接