2012-07-16 5 views
5

Làm cách nào để tôi có thể đạt được bố cục sau, với TextView là TextView với dấu chấm hết và Xem là wrap_content và ngay bên phải của TextView?Android - Ellipsize với chế độ xem phù hợp

Dưới đây là ba ví dụ về cách bố trí nên cư xử, với chiều rộng TextView khác nhau:

|[TextView] [View]    | 

|[TextView TextView] [View]  | 

|[TextView TextView T...] [View]| 

[Chỉnh sửa]

sau đây TableLayout cho hành vi đúng:

<TableLayout 
    android:id="@+id/upper_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_toLeftOf="@+id/alert_button" 
    android:shrinkColumns="0" 
    android:stretchColumns="2" > 

    <TableRow> 

     <TextView 
      android:id="@+id/name_textview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:ellipsize="end" 
      android:includeFontPadding="false" 
      android:lines="1" 
      android:paddingTop="2dp" 
      android:scrollHorizontally="true" /> 

     <TextView 
      android:id="@+id/unread_count_textview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:gravity="center" 
      android:includeFontPadding="false" 
      android:paddingBottom="0dp" 
      android:paddingLeft="5dp" 
      android:paddingRight="5dp" 
      android:paddingTop="2dp" /> 
    </TableRow> 
</TableLayout> 

Các TableLayout sử dụng 3 cột, cái cuối cùng là hack: thuộc tính strechColumns làm cho nó chiếm tất cả không gian có sẵn, vì vậy thứ hai TextView luôn luôn là ngay lập tức bên phải của người đầu tiên. Lần đầu tiên TextView bỏ qua chính xác do thuộc tính shrinkColumns.

Tuy nhiên, tôi không thích ý tưởng sử dụng số TableLayout để đạt được điều đó. Có ai biết cách nào tốt hơn không?

Cảm ơn

+0

Đây có phải là danh sách hay không? – Yury

+0

Vâng, đó là bố cục hàng. – gdelente

Trả lời

0

Hãy thử điều này (Tôi không thể kiểm tra xem nó bây giờ, nhưng để quan điểm của tôi nó cũng làm việc):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="6dip" > 

    <TextView 
     android:id="@+id/my_txtvw_txtvw" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:ellipsize="marquee" 
     android:maxLines="1" /> 

    <TextView 
     android:id="@+id/my_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/my_txtvw_txtvw" 
     android:maxLines="1" /> 

</RelativeLayout> 
+0

Cảm ơn nhưng không may với giải pháp này, TextView được đẩy ra khỏi màn hình khi TextView trái quá dài. – gdelente

0

Hãy thử điều này:

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

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@+id/my_view" 
     android:ellipsize="end" 
     android:maxLines="1" 
     android:layout_alignParentLeft="true" 
     android:text="blablabla blablabla" 
     android:layout_centerVertical="true" 
     android:gravity="center_vertical|left" /> 

    <TextView 
     android:id="@+id/my_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:maxLines="1" /> 

</RelativeLayout> 

Tôi không chắc chắn nếu bạn muốn android:ellipsize="end" hoặc android:ellipsize="marquee". Tôi nghĩ rằng bố cục sẽ hoạt động tốt với bạn, chỉ cần thay thế id giá trị elip cần thiết.

Cho tôi biết về tiến trình của bạn ...

+0

Cảm ơn yugidroid vì câu trả lời của bạn. Bạn đặt 'alignParentRight =" true "' trên chế độ xem thứ hai. Vấn đề là chế độ xem sẽ luôn được căn chỉnh ở bên phải. Tôi muốn "my_view" ở bên phải "text1". Khi TextView đầu tiên ("text1") quá dài để vừa với bên trong bố mẹ, "my_view" được căn phải và "text1" được bỏ qua. Xóa 'alignParentRight =" true "' không giúp được vì "text1" bị đẩy ra khỏi màn hình ở bên trái bởi my_view. – gdelente