Android TextView自动换行文本显示不全解决

某些情况下,TextView自动换行后,会出现每行结尾处显示不全的问题,

如图:

常见解决方案:

  1. 设置TextView的"ellipsize"属性为"end" 实测无效!
  2. 将TextView外部的Layout改为RelativeLayout 实测无效!
  3. 自定义TextView 过于繁琐且影响性能!

实际解决方案:

只需要设置TextView一个属性就可以解决,这个属性比较冷门:breakStrategy 意为换行策略,

将breakStrategy设置为"balanced"成功解决问题

代码如下:

html 复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/win_item_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:breakStrategy="balanced"
        android:ellipsize="end"
        android:maxLines="100"
        android:text="原文"
        android:textColor="@color/white"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/win_item_tv_t"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/win_item_tv"
        android:breakStrategy="balanced"
        android:ellipsize="end"
        android:maxLines="100"
        android:paddingTop="2dp"
        android:text="译文"
        android:textColor="#fbbf48"
        android:textSize="14dp" />

</RelativeLayout>
复制代码
修改后运行效果如下:
复制代码

复制代码
如果您感觉文章有用的话麻烦点个赞吧.
复制代码
复制代码
复制代码
相关推荐
砖厂小工4 小时前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
张拭心5 小时前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能
张拭心5 小时前
Android 17 来了!新特性介绍与适配建议
android·前端
Kapaseker7 小时前
Compose 进阶—巧用 GraphicsLayer
android·kotlin
黄林晴7 小时前
Android17 为什么重写 MessageQueue
android
阿巴斯甜1 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker1 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95271 天前
Andorid Google 登录接入文档
android
黄林晴1 天前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab2 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读