2014-09-25 125 views
1

我試圖在我的佈局中創建一個簡單的組件,其中有兩個水平相鄰的TextViews。右邊的那個應該從左邊的一個完成。我的代碼如下:TextView中被截斷的文本寬度設置爲wrap_content

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/container" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:textColor="@android:color/white" 
     android:textIsSelectable="false" 
     android:textSize="25sp" /> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:textColor="@android:color/white" 
     android:textSize="20sp" /> 

</LinearLayout> 

我以編程方式在視圖渲染後在每個TextView上設置文本。但是,有時文本在第一個TextView中無法正確顯示 - 我可以看到寬度已正確設置,因爲第二個TextView不在其旁邊,但文本被截斷而不是使用空格。如果我鎖定/解鎖設備以刷新屏幕,則文本顯示正確(不改變TextView的寬度)。

我試着改變這個使用RelativeLayout,但我看到相同的問題。

任何想法?

回答

0

雖然我不明白你究竟是什麼意思,會建議你在父視圖和android:layout_weight子視圖中使用weightSum屬性。同樣的,允許在父視圖內關於比率放置許多子視圖(如導航選項卡)。

爲如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/container" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="1" > 

    <TextView 
     android:id="@+id/text1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:textColor="@android:color/white" 
     android:textIsSelectable="false" 
     android:textSize="25sp" 
     android:layout_weight="0.4" /> //60% width 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:textColor="@android:color/white" 
     android:textSize="20sp" 
     android:layout_weight="0.6" /> //40% width 

</LinearLayout> 

也,不要忘記把寬度,如果孩子的意見,0dp。因爲這會導致忽略關於視圖寬度的計算。或者也可以將子視圖的寬度設置爲「match_parent」。任何其他財產寬度將無法正常工作。 (如果你希望兩個孩子的視圖設置layout_width爲0.5的兩個視圖的一半matchparent .. ithink明顯要注意的)ithink

Hopw它有幫助。