android - 自定义搜索栏(拇指大小、颜色和背景)

标签 android seekbar android-slider

我想在我的 Android 项目中使用这个搜索栏: enter image description here

这是我的搜索栏:

<SeekBar
        android:id="@+id/seekBar_luminosite"
        android:layout_width="@dimen/seekbar_width"
        android:layout_height="@dimen/seekbar_height"
        android:minHeight="15dp"
        android:minWidth="15dp"
        android:maxHeight="15dp"
        android:maxWidth="15dp"
        android:progress="@integer/luminosite_defaut"
        android:progressDrawable="@drawable/custom_seekbar"
        android:thumb="@drawable/custom_thumb" />

这是我的 custom_thumb.xml :

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <gradient
        android:angle="270"
        android:endColor="@color/colorDekraOrange"
        android:startColor="@color/colorDekraOrange" />

    <size
        android:height="35dp"
        android:width="35dp" />

</shape>

这是我的 custom_seekbar.xml :

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@android:id/background"
        android:drawable="@drawable/seekbar"/>
    <item android:id="@android:id/progress">
        <clip android:drawable="@color/colorDekraYellow" />
    </item>

</layer-list>

这是我的 seekbar.png(背景):

enter image description here

结果如下: enter image description here

栏内没有阴影和圆形边框...

我真的不明白我该怎么做。

最佳答案

全部在 XML 中完成(没有 .png 图像)。聪明的一点是 border_shadow.xml

所有关于 vectors 这些天...

截图:

seekbar
这是你的 SeekBar (res/layout/???.xml):

SeekBar

<SeekBar
    android:id="@+id/seekBar_luminosite"
    android:layout_width="300dp"
    android:layout_height="wrap_content"        
    android:progress="@integer/luminosite_defaut"
    android:progressDrawable="@drawable/seekbar_style"
    android:thumb="@drawable/custom_thumb"/>

让我们让它时尚(这样你以后可以轻松地自定义它):

风格

res/drawable/seekbar_style.xml:

<?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/border_shadow" >
    </item>

    <item 
        android:id="@android:id/progress" > 
        <clip 
            android:drawable="@drawable/seekbar_progress" />
    </item>
</layer-list>

拇指

res/drawable/custom_thumb.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/colorDekraOrange"/>
            <size
                android:width="35dp"
                android:height="35dp"/>
        </shape>
    </item>   
</layer-list>

进展

res/drawable/seekbar_progress.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list 
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        android:id="@+id/progressshape" >
        <clip>
            <shape 
                android:shape="rectangle" >
                <size android:height="5dp"/>
                <corners 
                    android:radius="5dp" />
                <solid android:color="@color/colorDekraYellow"/>        
            </shape>
        </clip>
    </item>
</layer-list>

阴影

res/drawable/border_shadow.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">       
    <item>      
        <shape> 
            <corners 
                android:radius="5dp" />
            <gradient
                android:angle="270"
                android:startColor="#33000000"
                android:centerColor="#11000000"
                android:endColor="#11000000"
                android:centerY="0.2"
                android:type="linear"
            />          
        </shape> 
    </item>
</layer-list>

关于android - 自定义搜索栏(拇指大小、颜色和背景),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41693154/

相关文章:

android - 将 Gradle 依赖项 res 复制到另一个 Gradle 依赖项

android - 为aosp添加原生服务

android - MVVMCROSS fragment 示例不适用于旋转

android - 如何在 PagerAdapter 中更新 textview(在 instantiateItem 中使用 inflater 之后)

c# - 仅当用户更改值时才会发生的 WPF Slider 事件

android - 如何显示离散 slider 的刻度线?

android - 求进度条Drawable Expand

android - 如何在android中制作自定义搜索栏?

android - 如何更改 Material slider 字体

android - 如何为 RangeSlider(valueFrom 和 valueTo)使用 Databinding 和 LiveData