Android布局使用layout_weight,layout_width和maxWidth

Blu*_*ell 5 android textview android-layout android-layout-weight

我正在尝试获得一行类似的文本

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>
Run Code Online (Sandbox Code Playgroud)

跟我一起这部分工作.

TextViews layout_width设置为0diplayout_weight1意味着它们将占用每个可用空间的50%.

设置singleLineto trueellipsizeto true意味着它们看起来像foofoo ...如果文本大于容器.

因此,我的结果(如果文本更长)应该是

foofoo .. - barbar ..

这是什么!这样可行.

现在我正在尝试修复的情况是,如果第一个TextView(id:text_1)的文本小于给定的50%layout_width="0dip",layout_weight="1"我想要它wrap_content.否则它看起来像:

foo 空白区域 - barbar ..

而且我要

foo - barbarbar

这就是我改变layout_width="wrap_content"和添加的原因,android:maxWidth="0dip"但这不起作用!它似乎无视我说wrap_content,仍然给这个观点50%!

我读了你需要添加android:adjustViewBounds="true"maxWidth工作,但这个没有影响.

big*_*nes 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>
Run Code Online (Sandbox Code Playgroud)

  • 删除android:stretchColumns ="0,2",它的工作原理.到目前为止!:-) 谢谢 (2认同)
  • 帮助我们两个.我是一个恩人:-D (2认同)