You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android MotionLayout Carousel中TextView滚动时末尾文本消失如何解决

问题根因

你遇到的文字丢失问题是轮播容器默认开启了子控件裁剪,动画过程中左侧TextView的边缘超出MotionLayout的可视边界时,超出部分就会被系统裁剪,所以会出现滚动时文字时隐时现、滚动结束后完全消失的现象。

解决方案

按以下优先级修改,完成前两步即可解决90%以上的同类问题:

  1. 关闭轮播容器的裁剪属性
    在你的main_fragment.xml中,给MotionLayout添加2个属性,允许子控件超出容器边界时正常显示:
<androidx.constraintlayout.motion.widget.MotionLayout
    android:id="@+id/motion_carousel"
    android:layout_width="0dp"
    android:layout_height="100dp"
    <!-- 新增以下两行 -->
    android:clipChildren="false"
    android:clipToPadding="false"
    app:layoutDescription="@xml/carousel_scene"
    ... 其余原有属性保持不变
>
  1. 给所有TextView添加单行展示配置
    避免文字因为宽度计算异常自动换行导致内容被隐藏,给每个TextView都添加以下属性:
<TextView
    ... 原有属性保持不变
    android:singleLine="true"
    android:ellipsize="none"
/>
  1. 可选优化方案
    如果修改后仍有极个别文字出现边缘裁剪,可以调整两个配置:
  • 适当调大dimen资源中carousel_names_horizontal_margin的数值,增大相邻TextView的间距,避免控件互相挤压
  • 在MainFragment.kt的populate方法中给文本末尾添加一个全角空格,避开裁剪边界:
override fun populate(view: View, index: Int) {
    (view as TextView).apply {
        text = nameList[index] + " "
    }
}

内容的提问来源于stack exchange,提问作者Buntupana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 22:57:03