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