Android BottomSheetDialogFragment中Button文本被截断问题求助
解决方案
直接在Button的XML布局中添加android:singleLine="false",同时根据需求设置android:maxLines(比如设为2或更大数值),即可让文本自动换行:
修改后的Button示例代码:
<Button android:id="@+id/b_answer_to_some_users_requests" style="@style/Widget.AppCompat.Button.Borderless" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginTop="10dp" android:text="Answer to some users requests!" android:textAllCaps="false" android:textColor="?attr/colorPrimary" android:textSize="20sp" android:singleLine="false" android:maxLines="2" app:layout_constraintEnd_toStartOf="@id/guideline" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tv_answer_to_request" />
如果项目中有多个类似Button需要换行,也可以修改全局样式,避免重复设置:
<style name="Widget.AppCompat.Button.Borderless" parent="Widget.AppCompat.Button.Borderless"> <item name="android:singleLine">false</item> </style>
原因分析
Button本质上继承自TextView,但Android系统给Button默认设置了android:singleLine="true"属性,这个属性会强制文本单行显示,超出宽度的内容就会被截断并显示省略号。而TextView的默认singleLine属性是false,所以能自动换行。
布局预览的渲染逻辑和真机运行时存在差异,预览时没有正确触发Button的默认单行限制,导致预览显示正常但真机运行时出现文本截断的问题。
内容的提问来源于stack exchange,提问作者Jawegiel
相关产品推荐
相关产品推荐

