2012-12-11 70 views
2

我有列表視圖。行有2個(以及更多的將來)文本視圖。我的目標是,如果第一個文本太長,則應顯示橢圓,但第二個文本視圖應始終可見。我臨時通過列(權重+文本視圖的百分比權重)解決了這個問題。但我的目標是:當文本短時,第二個文本視圖應該顯示在第一個旁邊(參見第一個附加屏幕,它是假的 - 由第一個文本視圖的硬編碼最大寬度創建)。問題是,當第一個文本太長時,第二個文本就會消失。我已經嘗試了很多配置與linearlayout,relativelayout,sublayouts等,但最後我卡住了。任何想法?這是簡單的XML爲行:Android - 長橢圓textview封面下一個textview

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/TextView01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:text="Some very very long long long long long long long" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/TextView02" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#C03518" 
     android:text="10" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
</LinearLayout> 

這是RelativeLayout的代碼不工作過:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/TextView01" 
     android:layout_alignParentLeft="true" 
    android:ellipsize="end" 
    android:singleLine="true"   
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Some very very long long long long long long long" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/TextView02" 
     android:layout_toRightOf="@id/TextView01" 
     android:layout_alignParentRight="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#C03518" 
     android:text="10" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
</RelativeLayout> 

Target

Reality

問候

+0

嘗試相對佈局和android:layout_toRightOf = 「idOftheLeftTextView」 將可能ellipsize它爲您服務! – Pavlos

+0

我同意上面的評論,但是這種方法要求第二個TextView具有'layout_alignParentRight =「true」'。 – Sam

+0

我也嘗試過使用RelativeLayout,但結果相同。我用relativelayout例子編輯了第一篇文章。也許我可以在運行時計算它嗎?例如計算第二個文本的寬度,然後設置爲第一個文本的最大寬度的區別 – Dibo

回答

0

您需要爲列設置權重。事情是這樣的:

<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 

<TextView 
    android:id="@+id/TextView01" 
    android:layout_width="0" 
    android:layout_height="wrap_content" 
    android:layout_weight="9" 
    android:ellipsize="end" 
    android:singleLine="true" 
    android:text="Some very very long long long long long long long" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/TextView02" 
    android:layout_width="0" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="#C03518" 
    android:text="10" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
</LinearLayout> 

這意味着第一列將佔據90%的屏幕和第二會佔用10%。 或者,您可以使用表格佈局並將stretch_columns設置爲0,1。 喜歡的東西:

<TableLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:stretch_columns="0,1" > 

<TableRow> 

<TextView 
android:id="@+id/TextView01" 
android:layout_width="0" 
android:layout_height="wrap_content" 
android:layout_weight="9" 
android:ellipsize="end" 
android:singleLine="true" 
android:text="Some very very long long long long long long long" 
android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
android:id="@+id/TextView02" 
android:layout_width="0" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:background="#C03518" 
android:text="10" 
android:textAppearance="?android:attr/textAppearanceMedium" /> 

</TableRow> 

</TableLayout>