【问题标题】:Android layout using layout_weight, layout_width and maxWidth使用 layout_weight、layout_width 和 maxWidth 的 Android 布局
【发布时间】:2011-02-10 16:49:20
【问题描述】:

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

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 工作,但这没有任何影响。

【问题讨论】:

    标签: android android-layout textview android-layout-weight


    【解决方案1】:

    这是我能做的最好的,尝试更改字符串以查看它是否按预期工作。

    <?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>
    

    【讨论】:

    • 删除 android:stretchColumns="0,2" ,它的工作原理。反正到目前为止! :-) 谢谢
    • @Blundell 哇,我是赏金猎人!很高兴它成功了,谢谢你的奖品。
    • 帮助我们俩。我是一个恩人:-D
    【解决方案2】:

    很难准确地说出您想要什么,但我相信答案是您无法做到这一点,除非您在代码中动态调整布局。这是因为您要求系统以多种不同的方式工作。

    首先,当视图中的文本数量相同时,您希望视图平均共享空间。

    其次,当另一个视图中的文本较少时,您希望视图平均共享空间。

    这里还有多种其他情况可以按照您想要的任何方式处理。然而,我想传达的是,您要求相同的布局以不同的方式处理事情,这就是为什么您无法通过单一布局实现这一目标。

    【讨论】:

    • 我想基本上说 maxWidth 是共享空间(因此省略),但通常只是 wrap_content。所以我会假设它会 wrap_content 除非 wrap_content 的倾角值 > 50%,那么它将使用 maxWidth(即 50%),不是吗?
    • unless wrap_content's dip value &gt; 50% then it would use maxWidth... 我真的不确定你想说什么。 wrap_content 不使用倾角值。
    • 是的,但 maxWidth 必须以某种方式与 layout_width 交互。所以我的意思是在后台它必须计算布局的宽度,如果它大于 maxWidth,则使用 maxWidth。所以我说的是 wrap_content ,但如果(在后台) wrap_content > 50% 使用 maxWidth。这不是它的工作原理吗?
    • 我想我明白你的意思了。问题是 wrap_content 的“理论倾角值”取决于,在这种情况下,视图中的文本。您根本无法想出一种适用于所有情况的布局,就像您尝试做的那样。请参阅我的原始答案。
    • 我不明白为什么 layout_width="wrap_content" & maxWidth="0dip" 不起作用 :-( 。在英语中,这将是'用 50% 的 maxWidth 包装内容'
    猜你喜欢
    • 1970-01-01
    • 2011-09-11
    • 2012-10-30
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多