android - 如何为 SeekBar 背景创建可绘制的图层列表?

标签 android drawable shapes layer-list

我设法创建了一个 xml 可绘制对象,它看起来像这样,它应该是这样的:

drawable

代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="line">
            <stroke android:color="@color/black"
                android:width="3dp" />
            <solid android:color="#f000"/>
        </shape>
    </item>

    <item android:top="1dp" android:left="360dp" android:bottom="1dp" android:right="0dp">
        <shape android:shape="oval">
            <size android:width="15dp"
                android:height="15dp"/>
            <solid android:color="@color/black"/>
        </shape>
    </item>

    <item android:top="1dp" android:left="246dp" android:bottom="1dp" android:right="114dp">
        <shape android:shape="oval">
            <size android:width="25dp"
                android:height="25dp"/>
            <solid android:color="@color/black"/>
        </shape>
    </item>

    <item android:top="1dp" android:left="114dp" android:bottom="1dp" android:right="246dp">
        <shape android:shape="oval">
            <size android:width="25dp"
                android:height="25dp"/>
            <solid android:color="@color/black"/>
        </shape>
    </item>

    <item android:top="1dp" android:left="0dp" android:bottom="1dp" android:right="360dp">
        <shape android:shape="oval">
            <size android:width="25dp"
                android:height="25dp"/>
            <solid android:color="@color/black"/>
        </shape>
    </item>

</layer-list>

然后我将它应用到我的 SeekBar:

<SeekBar
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:progressDrawable="@drawable/seekbar_style"
/>

但它不起作用......我只是明白了:

not working

所以现在我尝试以编程方式进行操作,因为它可能与左、上、右、下 DP 的问题有关……这是代码,但结果不是我所期望的……

int width = seekBar.getMeasuredWidth();

ShapeDrawable line = new ShapeDrawable();
line.getPaint().setStrokeWidth(3);
line.getPaint().setColor(Color.BLACK);

ShapeDrawable bubble1 = new ShapeDrawable(new OvalShape());
bubble1.setBounds(width, 1, 0, 1);
bubble1.getPaint().setColor(Color.WHITE);
bubble1.setIntrinsicHeight(25);
bubble1.setIntrinsicWidth(25);

//bubble2, bubble3, bubble4 with different colors and then:

Drawable[] layers = {line, bubble1, bubble2, bubble3, bubble4};
seekBar.setProgressDrawable(new LayerDrawable(layers));

not working again

有人能指出我正确的方向吗?我做错了什么?

最佳答案

我不认为单靠层列表能让您到达任何地方。如果您不想创建自己的自定义搜索栏,我会检查一些名称中带有 SeekBar 的自定义库,例如:

  1. https://github.com/channguyen/range-slider-view
  2. https://github.com/warkiz/IndicatorSeekBar
  3. https://github.com/alshell7/terminal-seekbar

关于android - 如何为 SeekBar 背景创建可绘制的图层列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47814827/

相关文章:

android - HashMap<String, Drawable> mDrawables;

android - ConstraintLayout - 如何防止圆圈将其他 View 推离页面?检查最小尺寸?

android - 如何根据日期和时间android每半小时重置一次计数?

Android WebView 不显示 Google-plus 幻灯片菜单

android - "You need to use a theme.appcompat theme"以Theme.AppCompat为主题,Sdk v23

带有启动画面的 Android Material 动画,如 Palabre 应用程序

android - 另一个 : List of Android Holo Drawable Icons?

database - 将 Drawable 引用保存到 SQLite 数据库中

Android:当设置为背景时,如何使用 layer-list 和 shape 元素绘制水平线?

android - 如何在运行时使用 Android 上的形状属性更改 TextView 的颜色?