我有带有自定义可绘制对象的 SeekBar。 Progress 元素并没有完全从左侧呈现,而是移到了右侧。我怎样才能避免这种情况?
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@android:id/summary"
android:background="@null"
android:progressDrawable="@drawable/seekbar"
android:thumb="@drawable/seekbar_thumb"/>
搜索栏.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@android:id/background"
android:drawable="@drawable/seekbar_background"/>
<item android:id="@android:id/progress">
<clip android:drawable="@drawable/seekbar_progress" />
</item>
</layer-list>
seekbar_background.xml
<?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="@color/colorNeutral" />
</shape>
seekbar_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
<stroke
android:width="4dp"
android:color="@color/colorAccentDark" />
</shape>
编辑:
如评论中所述,如果两个宽度相同,问题就会消失。但是我需要通过 drawable 使它具有两种不同的宽度,这可能吗?也许有不同的形状而不仅仅是线条?
原文由 Michal 发布,翻译遵循 CC BY-SA 4.0 许可协议
我无法解释为什么在绘制笔划时会出现这个问题,我认为这与笔划宽度有关,但我还没有找到来源来验证。
修复叠加层
要解决手头的问题,您只需在左侧设置
inset
。1dp
或2dp
的值都可以正常工作,搜索栏将被正确绘制。注意: 使用这种方法应该没有背景可能太短并且在进度值较低时不可见的风险,因为在任何情况下拇指都会覆盖并隐藏它。