【问题标题】:Android - Seekbar tick mark color not able to changeAndroid - Seekbar 刻度线颜色无法更改
【发布时间】:2021-02-11 06:45:33
【问题描述】:

我正在使用离散搜索栏。我想更改刻度线的颜色。我正在尝试如下。

<SeekBar
        android:id="@+id/seekBar"
        style="@style/Widget.AppCompat.SeekBar.Discrete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:thumbTint="@color/colorPrimary"
        android:tickMarkTint="@color/colorAccent"/>

我还创建了样式并应用于tickMark。

drw_bg_tickmark.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:tint="@color/colorAccent">
    <corners android:radius="4dp"/>
    <size android:width="10dp"
        android:height="10dp" />
    <solid android:color="@color/colorAccent" />
</shape>

应用于搜索栏

android:tickMark="@drawable/drw_bg_tickmark"

但我无法删除搜索栏中的灰色刻度。请帮我解决这个问题。

【问题讨论】:

    标签: android seekbar


    【解决方案1】:

    如果你想删除灰色刻度/球,你可以删除样式,然后将你的drawable应用到android:tickMark

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:tickMark="@drawable/drw_bg_tickmark"
        android:thumbTint="@color/colorPrimary"
        android:tickMarkTint="@color/colorAccent"/>
    

    【讨论】:

    【解决方案2】:

    为你的搜索栏创建一个样式

     <resources>
            <style name="SeekBarWithoutSteps" parent="Base.Widget.AppCompat.SeekBar.Discrete">
                <item name="tickMark">@null</item>
            </style>
        </resources>
    
    
    
    <SeekBar
            android:id="@+id/seekBar"
            style="@style/SeekBarWithoutSteps"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="10"
            android:thumbTint="@color/colorPrimary"
            android:tickMarkTint="@color/colorAccent"/>
    

    【讨论】:

    • 我们可以通过 java 以编程方式执行 @null 吗?
    • seekBar.setTickMark(getResources().getDrawable(R.drawable.drw_bg_tickmark));我们可以动态设置tickMark drawable。这需要 API 级别 24 及以上。
    • 感谢 Krishan 的回答。这也有效。但是当我以编程方式尝试设置时,我遇到了一些问题。所以我要和Zain ans一起去。如果对以下问题有任何想法,请告诉我。 stackoverflow.com/questions/66125645/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2020-08-03
    • 2017-09-16
    相关资源
    最近更新 更多