Android - 如何更改默认 SeekBar 厚度?

标签 android seekbar android-seekbar

我有这个 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 的厚度吗?如果是,怎么做?

最佳答案

你必须改变 SeekBarprogressDrawablethumb 来调整它的粗细:

<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

关于Android - 如何更改默认 SeekBar 厚度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32212259/

相关文章:

java - Android 搜索栏值更改按钮

java - SeekBar.java 有错误

Android 应用程序无法连接到 LAN 服务器

java - 是什么导致 UsbRequest 从队列方法抛出 IO 异常?

java - 搜索栏无法正常工作

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

android - 自定义范围搜索栏

java - 为自定义 seekBar 动态更改 LinearGradient 颜色比例

java - 使用 cookie 从 PHP 读取 JSON

Android 磁力计计算高度