开发者

How to get a layout where one text can grow and ellipsize, but not gobble up the other elements on the layout

I've read some of the other posts here such as Two TextViews side by side, only one to ellipsize? but I'm still having an issue with my layout.

I have a list item layout, and I want each item in the list to look like this:

| (Expanding TextView #1) (TextView #2)   &开发者_StackOverflownbsp;                                                      (Image) |

TextView #2 and Image must always be visible.

Right now I'm using the following layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainItem"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="@drawable/myBackground"
android:onClick="onClick"
android:longClickable="true">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:layout_gravity="center_vertical|left"
    android:gravity="center_vertical|left"
    android:layout_toLeftOf="@+id/myImage"
    android:layout_alignParentLeft="true">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="14dp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="0"/>
     <TextView
        android:id="@+id/testView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>    
 <ImageView
    android:id="@+id/myImage"
    android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:layout_gravity="center_vertical"
    android:layout_alignParentRight="true"
    android:paddingRight="14dp"
    android:onClick="onClick"
    android:src="@drawable/myIcon"/>
</RelativeLayout>

I've read from the other posts that adding a layout_weight="1" to TextView#1 will force TextView #2 to be shown, and it does, but the problem is that this forces TextView #2 to be right-aligned because it causes TextView #1 to expand even when it doesn't have to.

I'm pretty stumped on this now... could anyone help? :)

UPDATE

I was able to fix this by using a TableLayout and the shrink & stretch column properties. By playing around with that it finally worked the way I wanted it to.


This will truncate (if needed) the text in the first TextView, keep the text in the second TextView as is, and align and keep as is the text in the third TextView.

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="0"
        android:stretchColumns="2">

        <TableRow>

            <TextView
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLines="1"/>

            <TextView
                android:layout_height="wrap_content"
                android:maxLines="1"/>

            <TextView
                android:layout_height="wrap_content"
                android:gravity="end"
                android:maxLines="1"/>
        </TableRow>
    </TableLayout>


If I were you I'd probably switch the row from LinearLayout to RelativeLayout, that way you can align image to the parent right, butt textview2 right up next to it and just align textview1 with the parent left and it can resize without affecting the other two fields.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜