如何通过添加填充android:ellipsize="marquee"来修复文本溢出TextView的问题

8
在一些安卓设备上(如搭载Android L和M的LG Google Nexus 5),一个带有android:ellipsize="marquee"和padding的TextView会导致文本溢出TextView。这种情况发生在TextView的右侧,但是左侧没有发生,而且padding被应用于左右两侧。

screenshot

它不会发生在三星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" />

我该怎么做来修复或解决这个问题?

1个回答

4
你的问题是安卓系统的一个漏洞,已经报告并指派到了安卓开源项目中:
你可以采用以下解决方法:
<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>

所以,这个想法是使用包装容器,具有填充、边距和背景。(如果你只有几个这样的视图,它不应该会对性能产生太大影响)

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

  1. First try to remove attribute one by one checking the result
  2. You can try to specify paddings on your container instead of text views
  3. From your layout it seems that you can try something like this instead of "match_parent" on your text views:

    <LinearLayout android:orientation="horizontal" ...>
        <TextView android:layout_width="0dp" android:layout_weight="1" ... />
        <TextView android:layout_width="0dp" android:layout_weight="1" ... />
    </LinearLayout>
    

谢谢Gregory,但我现在没有时间验证,但如果在5天内没有其他人回答,我会给你悬赏..否则悬赏将消失:P .. '但目前'主要问题仍然存在..为什么三星Galaxy S3和S6与Android K和L不会发生。 - Wojtek Dmyszewicz
@WojtekDmyszewicz想提供一些如何开始修复/解决这个问题的想法。如果您可以提供有问题的代码(我指的是容器+文本视图),我会再深入研究一下。 - GregoryK
如果问题没有被回答,为什么要设置悬赏?TextView位于一个LinearLayout内(包含标题TextView),该LinearLayout是对父RelativeLayout底部进行了对齐。问题的核心是:为什么在某些设备上,会在一侧溢出。请证明解决方案3可行。 - Jesse van Muijden
@JessevanMuijden 看来我误解了问题。我以为 Wojtek 有两个文本视图。我会尽快更新我的答案。 - GregoryK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接