开发者

Trouble with layout in android (ellipsized text between two buttons)

Trying to make toolbar with two buttons on both sides and header between. Header should be ellisized when text is too long, like this:

[Button] Some quite long header te... [Button]

I've tried several solutions, but none help. My last try was something like this:

<TableLayout android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:background="#444"
             android:stretchColumns="1">
    <TableRow android:background="#777" android:minHeight="60dp">
        <Button android:text="Left"/>
        <TextView android:text="Center this very long text and ellisize"
                  android:background="#f00"
                  android:lines="1"
                  android:ellipsize="end"
                  android:scrollHorizontally="true"
                  android:gravity="center"
                />
        <Button android:text="Right"/>
    </TableRow>
</TableLayout>

But right button still goes away from screen...

UPDATE: The solution is:

<RelativeLayout android:layout_width="fill_parent" android:layout_height="64dip">
    <Button android:id="@+id/btnLeft"
            android:te开发者_运维问答xt="Left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <Button android:id="@+id/btnRight"
            android:text="Right"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <TextView android:text="Center this very long text and ellisize"
              android:layout_width="fill_parent" android:layout_height="fill_parent"
              android:background="#f00"
              android:lines="1"
              android:ellipsize="end"
              android:gravity="center"
              android:scrollHorizontally="true"
              android:layout_toLeftOf="@id/btnRight"
              android:layout_toRightOf="@id/btnLeft"
            />
</RelativeLayout>

UPDATE 2: And the second solution using TableLayout:

<TableLayout android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:background="#444"
             android:stretchColumns="1"
             android:shrinkColumns="1">
    <TableRow android:background="#777" android:minHeight="60dp">
        <Button android:text="Left"/>
        <TextView android:text="Center this very long text and ellisize"
                  android:background="#f00"
                  android:lines="1"
                  android:ellipsize="end"
                  android:scrollHorizontally="true"
                  android:gravity="center"
                />
        <Button android:text="Right"/>
    </TableRow>
</TableLayout>


Try using a relative layout instead of a table layout. If you set up the two buttons to be aligned left and right respectively, you can set the text field to span between the two buttons with the layout_alignRight and layout_alignLeft properties. You can look at the example code for the RelativeLayout given on the android development website.


While the RelaviveLayout should work, another option is to add a LinearLayout set to Horizontal. Set the three widgets to layout_width:"fill_parent" and give the TextView and both Buttons a weight (1 for each if you want them to equally take up three parts of the screen).

Not at a computer to test it right now, but should theoretically work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜