【问题标题】:Layout views with dynamic width具有动态宽度的布局视图
【发布时间】:2012-06-19 03:13:21
【问题描述】:

我有一个布局,两个孩子一个接一个,像这样:

|<view1><text1>                      |

&lt;view1&gt; 可能会改变它的宽度。所以,我希望它在两个视图都停留在屏幕上时增加:

|<really_wide_view1><text1>          |

但是当右边没有更多空间时,它会停止:

|<reeeeeeeeeeeally_wide_vi...><text1>|

有没有简单的方法可以做到这一点?

现在我尝试使用这个布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF00aa00" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="loooooooooooooooooooooooooooooooooooong text" />
    </LinearLayout>

    <TextView
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#FFaa0000"
        android:minWidth="30dp"
        android:singleLine="true"
        android:text="test" />

</LinearLayout>

但结果是一样的:

【问题讨论】:

  • LinearLayout 中使用layout_weight 怎么样?
  • 我尝试将layout_weight="1" 设置为第二个视图 - 但是当第一个足够大时 - 第二个比屏幕更右边
  • 我也尝试使用android:minWidth 进行第二次查看 - 没有结果
  • 请务必使用layout_width=0dp,以便它自动计算可用宽度。
  • 查看这个问题的答案:stackoverflow.com/questions/3995825/…

标签: android android-layout


【解决方案1】:

我可以使用以下代码(您的修改版本)解决您的问题:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FF00aa00" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text" 
            />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="0"
        android:background="#FFaa0000"
        android:text="testtest" />
</LinearLayout>

这是相同的屏幕截图:

如果上面的代码解决了问题,请告诉我。

【讨论】:

  • 这解决了长文本的问题,但现在它对短文本不起作用。我找到了 TableView 的解决方案,我会在五分钟内发布它
【解决方案2】:

尝试在 RelativeLayout 中提供两个视图,并使视图布局宽度和作为 wrap_content 并将方向设置为水平。我想它会解决你的问题。

【讨论】:

  • 这不起作用。如果视图之间没有关系 - 它会相互覆盖。如果我将toRightOf 用于第二个视图 - 它会远离屏幕
【解决方案3】:

这是TableLayout 的解决方案,但它看起来很脏。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:shrinkColumns="0" >

    <TableRow>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </TableRow>

</TableLayout>

它适用于任何长度的文本视图。

【讨论】:

  • 我打算发布一个答案,但你想出了TableLayout 解决方案。我在这里stackoverflow.com/questions/9448113/… 回答了类似的问题。基本上不需要添加LinearLayout,您只需将android:shrinkColumns="0" android:stretchColumns="1" 添加到您的TableLayout
  • LinearLayout 像容器一样走到这里,里面会有一些复杂的视图。