簡體   English   中英

文本在TextView中被截斷,寬度設置為wrap_content

[英]Text being truncated in TextView with width set to wrap_content

我試圖在我的布局中創建一個簡單的組件,在該組件中水平相鄰的兩個TextViews。 右邊的一個應該從左邊的一個結束處開始。 我的代碼如下:

<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不在旁邊,而是文本被截斷而不是使用空格。 如果我鎖定/解鎖設備以刷新屏幕,則文本將正確顯示(不改變TextViews的寬度)。

我嘗試將其更改為使用RelativeLayout,但是我看到了相同的問題。

有任何想法嗎?

盡管我不理解您的確切意思,但建議您在父視圖中使用weightSum屬性,在子視圖中使用android:layout_weight。 同樣,可以將許多子視圖相對於比率放置在父視圖中(例如導航選項卡)。

例如:

<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”。 寬度的任何其他屬性將不起作用。 (並且如果您希望兩個子視圖的一半matchparent都將layout_width設置為0.5,則兩個視圖都是這樣。

霍普幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM