如何修复文本溢出TextView与填充android:ellipsize ="marquee"

Woj*_*icz 7 android ellipsis textview nexus-5

在某些Android设备上(带有Android L和M的LG Google Nexus 5),带有android:ellipsize ="marquee"的文本视图和填充会导致文本溢出textview.它发生在右侧但不在TextView的左侧,而填充应用于左侧和右侧.

截图

三星Galaxy S3和S6分别不会出现在Android K和L上.

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:freezesText="true"
android:alpha="0.85"
android:background="@color/by433_gray_darker"
android:textColor="@color/white"
android:textSize="11sp" />
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能解决或解决这个问题?

Gre*_*ryK 4

您的问题是 Android 的一个错误,并且已经在 Android 开源项目上报告和分配:

您的解决方法可能如下所示:

<FrameLayout android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:paddingTop="2dp"
             android:paddingBottom="2dp"
             android:paddingLeft="4dp"
             android:paddingRight="4dp"
             android:layout_marginBottom="1dp"
             android:background="@color/by433_gray_darker">
    <TextView
        android:id="@+id/textId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:freezesText="false"
        android:alpha="0.85"
        android:text="super nice text text text text text text text text text text text text text text text text text"
        android:textColor="@color/white"
        android:textSize="11sp" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

因此,我们的想法是拥有一个带有填充、边距和背景的包装容器。(如果您只有几个这样的视图,那么性能开销应该不会太大)


原始错误答案(尽管有两个 TextView) 该问题可能是由于 TextView 上的多个属性组合所致。以下是一些建议:

  1. 首先尝试一一删除属性检查结果
  2. 您可以尝试在容器上指定填充而不是文本视图
  3. 从您的布局来看,您似乎可以在文本视图上尝试类似的操作,而不是“match_parent”:

    <LinearLayout android:orientation="horizontal" ...>
        <TextView android:layout_width="0dp" android:layout_weight="1" ... />
        <TextView android:layout_width="0dp" android:layout_weight="1" ... />
    </LinearLayout>
    
    Run Code Online (Sandbox Code Playgroud)