2012-07-16 45 views
5

如何實現以下佈局,其中TextView是具有ellipsize結尾的TextView,並且視圖是wrap_content並緊接着TextView的右側?Android - Ellipsize右對齊視圖

下面是佈局應該如何表現的三個例子,具有不同的TextView寬度:

|[TextView] [View]    | 

|[TextView TextView] [View]  | 

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

[編輯]

以下TableLayout提供了正確的行爲:

<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> 

TableLayout使用3列,最後是黑客:strechColumns屬性使它佔用所有可用空間,所以第二個TextView總是立即在第一個的權利。 由於shrinkColumns屬性,所以第一個TextView橢圓。

但是,我不喜歡使用TableLayout來實現這一點的想法。 有人知道更好的方法嗎?

感謝

+0

這是一個列表嗎? – Yury 2012-07-16 16:43:16

+0

是的,這是一個行佈局。 – gdelente 2012-07-16 17:05:47

回答

0

試試這個(我現在不能檢查它,但我的角度來看,這應該工作):

<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

感謝但不幸的是,使用此解決方案時,當左邊的TextView太長時,正確的TextView會從屏幕上被推下。 – gdelente 2012-07-16 19:14:50

0

試試這個:

<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> 

我不是當然,如果你想要android:ellipsize="end"android:ellipsize="marquee"。我認爲這個佈局對你來說可以正常工作,只需要替換所需的ellipsize值id即可。

讓我知道你的進步...

+0

感謝yugidroid爲您的答案。 您在第二個視圖中放置了alignParentRight =「true」'。問題在於視圖總是與最右側對齊。 我希望「my_view」恰好在「text1」的右側。一旦第一個TextView(「text1」)太長而無法放入父級內部,「my_view」就會對齊並且「text1」被省略。 刪除'alignParentRight =「true」'沒有幫助,因爲「text1」被my_view從屏幕左側推出。 – gdelente 2012-07-17 07:47:15