Android Java中使用Drawable实现水平虚线不显示问题求助
Android水平虚线不显示的排查与修复
下面列出关键问题及修复方案:
1. Drawable文件结构不完整
你的Drawable代码缺少根标签的起始部分和Android命名空间,完整的Drawable代码应该是:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <stroke android:width="2dp" android:color="#000000" android:dashWidth="4dp" android:dashGap="4dp"/> </shape>
2. View高度设置过小
你给View的android:layout_height="1dp",但Drawable中线条宽度是2dp,View高度小于线条宽度会导致线条无法显示,需将高度调整为至少等于线条宽度:
android:layout_height="2dp"
3. 硬件加速导致虚线失效
Android默认硬件加速会让虚线的间断效果失效,给View添加属性禁用硬件加速:
android:layerType="software"
4. 错误引用Drawable的属性
View的android:drawable属性不适合设置背景线条,应改用android:background来引用虚线Drawable。
修复后的fragment.xml代码片段:
android:id="@+id/line1" android:layout_width="match_parent" android:layout_height="2dp" android:layout_marginHorizontal="16dp" android:layout_marginTop="8dp" android:background="@drawable/line_seperator_dotted" android:layerType="software" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/compartmentHeader"
按以上修改后,水平虚线即可正常显示。
内容的提问来源于stack exchange,提问作者Akshay A
相关产品推荐
相关产品推荐

