简体   繁体   English

文本在TextView中被截断,宽度设置为wrap_content

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

I'm trying to create a simple component in my layout, where there are two TextViews horizontally next to each other. 我试图在我的布局中创建一个简单的组件,在该组件中水平相邻的两个TextViews。 The one on the right should start where the one on the left finishes. 右边的一个应该从左边的一个结束处开始。 My code for this is as follows: 我的代码如下:

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

I programmatically set the text on each TextView after the view has rendered. 呈现视图后,我以编程方式在每个TextView上设置了文本。 However, sometimes the text does not display correctly in the first TextView- I can see that the width has been set correctly, as the second TextView is not next to it, but the text is truncated rather than using the space. 但是,有时文本不能在第一个TextView中正确显示-我可以看到宽度设置正确,因为第二个TextView不在旁边,而是文本被截断而不是使用空格。 If I lock/unlock the device to refresh the screen then the text displays correctly (without the widths of the TextViews changing). 如果我锁定/解锁设备以刷新屏幕,则文本将正确显示(不改变TextViews的宽度)。

I've tried changing this to use a RelativeLayout, but I see the same issue. 我尝试将其更改为使用RelativeLayout,但是我看到了相同的问题。

Any ideas? 有任何想法吗?

Although i dont understant what exactly you mean, would suggest you to use weightSum property in the parent view and android:layout_weight in child views. 尽管我不理解您的确切意思,但建议您在父视图中使用weightSum属性,在子视图中使用android:layout_weight。 The same allows to put many child views inside a parent view with respect to ratio (like navigation tabs). 同样,可以将许多子视图相对于比率放置在父视图中(例如导航选项卡)。

for eg : 例如:

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

also, dont forget to put the width if child views as 0dp. 另外,如果子视图为0dp,请不要忘记设置宽度。 as that will result in ignoring the calculations regarding the width of view. 因为那样会导致忽略关于视线宽度的计算。 or you can set the width of child view as "match_parent" as well. 或者您也可以将子视图的宽度设置为“ match_parent”。 any other property to width will not work. 宽度的任何其他属性将不起作用。 (and if you want half matchparent for both child views set layout_width to 0.5 both views.. ithink thats obvious to note) (并且如果您希望两个子视图的一半matchparent都将layout_width设置为0.5,则两个视图都是这样。

Hopw it helps. 霍普帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM