Android:自定义 SeekBar 图层列表未按预期工作

标签 android seekbar

我有一个自定义的 SeekBar:

<SeekBar 
   android:id="@+id/seekBar"
    android:layout_width="@dimen/seekbar_width"
    android:layout_height="wrap_content"
    android:paddingTop="15px"
    android:paddingBottom="15px"
    android:secondaryProgress="0"
    android:progress="0"
    style="@style/CustomSeekBar"/>

采用这种风格:

<style name="CustomSeekBar">
    <item name="android:layout_width">@dimen/seekbar_width</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:progressDrawable">@drawable/custom_seekbar_progress</item>
    <item name="android:thumb">@drawable/slider_bar_grip</item>
</style>

我已经尝试了两种类型的 if layer lists with it。首先是九张补丁图片:

<?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/slider_bar_track"></item>
    <item android:id="@android:id/secondaryProgress"    android:drawable="@drawable/slider_bar_track"></item>
    <item android:id="@android:id/progress"             android:drawable="@drawable/slider_bar_hlt"  ></item>
</layer-list>

其次是项目和渐变:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:bottomLeftRadius="4px" android:topLeftRadius="4px" android:topRightRadius="4px" android:bottomRightRadius="4px"/>
            <gradient android:startColor="@color/dark_grey" android:endColor="@color/light_grey" android:angle="-90.0" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:bottomLeftRadius="4px" android:topLeftRadius="4px" android:topRightRadius="4px" android:bottomRightRadius="4px"/>
            <gradient android:startColor="@color/dark_grey" android:endColor="@color/light_grey" android:angle="-90.0" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:bottomLeftRadius="4px" android:topLeftRadius="4px" android:topRightRadius="4px" android:bottomRightRadius="4px"/>
                <gradient android:startColor="@color/orange" android:endColor="@color/orange" android:centerColor="@color/light_orange" android:angle="90.0" />
            </shape>
        </clip>
    </item>
</layer-list>

它应该是这样的:enter image description here

图片是这样的:enter image description here 高度太高(进度、背景和缩略图 png 均为 12 像素高,其中背景和进度 png 在顶部和底部具有透明像素以使其高度相同)enter image description here enter image description here enter image description here我在 res 文件夹中有这三个文件,所有密度都具有相同的大小(正如我希望的那样)。 此外,进度始终延伸到 SeekBar 的整个宽度。我尝试更改图层列表的顺序但无济于事。

当使用层列表中的形状时,它看起来像这样:enter image description here 至少在这里,进度是正确的,但高度仍然太高。

在这种情况下,我需要所有密度的高度相同(12px,而不是 dp)。屏幕截图来自 xhdpi 设备。进度图的高度与 mdpi 设备上的拇指相同。我怎样才能使它们在所有设备上看起来都一样,并使可绘制的进度小于拇指?

最佳答案

问题是在进度和背景 png 的左边框上定义的可拉伸(stretch)区域。

可拉伸(stretch)区域目前大约在左边缘中间有 1 个像素宽。这意味着您的 png 的水平中心线将被拉伸(stretch)。但是您需要的是“拉伸(stretch)透明区域”。

去做吧:

  • 移除左边缘中间的黑色像素。
  • 在左边缘大约 y=3 和 y=9 处添加 2 个黑色像素(确保它们都在 png 的透明区域中)

关于Android:自定义 SeekBar 图层列表未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14925993/

相关文章:

android - 无法在 Android 中设置 Firebase 数据库

Android SeekBar - 如何在右边变小,在左边变大

android - Android 中的垂直搜索栏

Android seekbar - 拇指在设置其可绘制对象后不可见

Android - 设置搜索栏进度始终留给拇指

android - 在 GridView 中从 SD 卡目录加载图像

android - Firebase 动态链接广告系列跟踪不起作用

java - "How to fix this post HTTP request using Android Studio?"

java - 在 Intellij 中添加 Maven 依赖

android - 如何实现价格范围搜索栏?