【问题标题】:Text being truncated in TextView with width set to wrap_content在 TextView 中被截断的文本,宽度设置为 wrap_content
【发布时间】:2014-09-25 15:51:24
【问题描述】:

我正在尝试在我的布局中创建一个简单的组件,其中有两个水平相邻的 TextView。右边的应该从左边的结束的地方开始。我的代码如下:

<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,但我看到了同样的问题。

有什么想法吗?

【问题讨论】:

    标签: android android-layout textview


    【解决方案1】:

    虽然我不明白你的意思,但建议你在父视图中使用 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 两个视图.. 我认为这很明显)

    希望有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多