安卓 - 如何更改默认SeekBar的厚度?

45

我有这个SeekBar:

        <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="26dp"
        android:id="@+id/seekbar"
        android:visibility="gone"
        android:maxHeight="3dp"/>

我尝试使用maxHeight更改这个SeekBar的厚度,但是没有任何变化。它看起来像这样:

enter image description here

我想将这个SeekBar的厚度更改为3dp,并使其看起来像这样: enter image description here

因此,我的问题是是否可以以某种方式更改SeekBar的厚度?如果可以,如何更改?


你有没有想到任何解决这个问题的办法? - Marek M.
6个回答

75

要调整SeekBar的厚度,您需要更改progressDrawablethumb

<SeekBar
    android:id="@+id/seekBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/indSeekBar"
    android:layout_marginTop="48dp"
    android:progressDrawable="@drawable/seek_bar"
    android:thumb="@drawable/seek_thumb"
    />

在drawable文件夹中添加seek_bar.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape
            android:shape="line">
            <stroke
                android:color="@color/seek_bar_background"
                android:width="@dimen/seek_bar_thickness"/>
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape
                android:shape="line">
                <stroke
                    android:color="@color/seek_bar_secondary_progress"
                    android:width="@dimen/seek_bar_thickness"/>
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape
                android:shape="line">
                <stroke
                    android:color="@color/seek_bar_progress"
                    android:width="@dimen/seek_bar_thickness"/>
            </shape>
        </clip>
    </item>

</layer-list>
seek_thumb.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size
        android:height="@dimen/seek_bar_thumb_size"
        android:width="@dimen/seek_bar_thumb_size"
        />
    <solid android:color="@color/seek_bar_progress"/>
</shape>

添加到维度资源(更改此参数以调整厚度):

<dimen name="seek_bar_thickness">4dp</dimen>
<dimen name="seek_bar_thumb_size">20dp</dimen>

添加颜色资源:

<color name="seek_bar_background">#ababab</color>
<color name="seek_bar_progress">#ffb600</color>
<color name="seek_bar_secondary_progress">#3399CC</color>

enter image description here


1
你能解释一下如何“添加尺寸资源”吗?我在Android Studio的res文件夹中看到了“drawable”,“layout”和“values”,但没有“dimension”。 - stevendesu
3
@stevendesu 在values资源文件夹中创建dimens.xml文件,技术上你可以在任何XML文件中声明任何字符串、颜色、尺寸资源,只要它们被包含在<resources></resources>标签中。 - Vaibhav Jani

14

你应该这样做:

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:thumb="@drawable/seek"
    android:progressDrawable="@drawable/seek_style"
    />

拇指是可以拖动的圆圈,而progressDrasable则是进度条的样式。

seek_style.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient
                android:startColor="#ff9d9e9d"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.75"
                android:endColor="#ff747674"
                android:angle="270"
                />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:startColor="#80ffd300"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:angle="270"
                    />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:startColor="#ff0099CC"
                    android:centerColor="#ff3399CC"
                    android:centerY="0.75"
                    android:endColor="#ff6699CC"
                    android:angle="270"
                    />
            </shape>
        </clip>
    </item>
</layer-list>

seek.xml:

寻找.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <size
        android:width="20dp"
        android:height="20dp"
        />
    <solid android:color="#60f0"/>
</shape>

12
你可以使用Material Components库提供的滑块(Slider)
使用app:trackHeight="xxdp"(默认值为4dp)来更改轨道栏的高度。
    <com.google.android.material.slider.Slider
        app:trackHeight="12dp"
        .../>

enter image description here

如果你想改变滑块半径,可以使用app:thumbRadius属性:

        <com.google.android.material.slider.Slider
            app:trackHeight="12dp"
            app:thumbRadius="16dp"
            ../>
           

enter image description here

需要使用库的版本为1.2.0


滑块似乎不支持诸如次要进度之类的功能。 - Arno Schoonbee

1
我用以下代码解决了问题: android:scaleX="2"

1

只需设置 scaleY 以调整拖动条的高度,并设置内边距以去除外部填充。

 <androidx.appcompat.widget.AppCompatSeekBar
        android:id="@+id/seekbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:paddingStart="0sp"
        android:paddingTop="0sp"
        android:scaleY="3" />

-5

您可以使用属性minHeightmaxHeight更改SeekBar的厚度。

android:minHeight="5dp"

android:maxHeight="5dp"

以下是在xml中如何实现:

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="26dp"
    android:id="@+id/seekbar"
    android:visibility="gone"
    android:minHeight="5dp"
    android:maxHeight="5dp"/>

这实际上是有效的,我可以问一下那些给我点踩的人为什么认为这是错误的吗? - Micer
4
这是一种“锤子修复”方法。有时它不起作用并且可能导致视觉上的错误。 - Ricardo Alves

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接