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

时间:2017-01-17 09:15:49

标签: android seekbar

我想在我的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

栏中没有阴影,也没有圆形边框......

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

11 个答案:

答案 0 :(得分:65)

全部在XML完成(无.png张图片)。聪明的位是 border_shadow.xml

所有关于vectors这几天......

屏幕截图:

seekbar
这是您的SeekBarres/layout/???.xml):

搜索栏

<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 /抽拉/ 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>

答案 1 :(得分:40)

  • 首先,使用android:splitTrack="false"来解决拇指的透明度问题。

  • 对于seekbar.png,您必须使用 9补丁。圆形边框和图像阴影会很好。

答案 2 :(得分:11)

  

栏中没有阴影和圆形边框

您正在使用图像,因此最简单的解决方案是将您的船划船,

你无法手动给出高度,是的,你可以确保它有足够的空间来显示你的完整图像视图

  • 最简单的方法是android:layout_height="wrap_content"
  • 使用SeekBar
  • 要获得更清晰的圆形边框,您可以轻松使用与其他颜色相同的图像。

我对Photoshop不好,但我设法编辑了一个背景测试

seekbar_brown <子> seekbar_brown_to_show_progress.png

<SeekBar
    android:splitTrack="false"   // for unwanted white space in thumb
    android:id="@+id/seekBar_luminosite"
    android:layout_width="250dp"   // use your own size
    android:layout_height="wrap_content"
    android:minHeight="10dp"
    android:minWidth="15dp"
    android:maxHeight="15dp"
    android:maxWidth="15dp"
    android:progress="50"
    android:progressDrawable="@drawable/custom_seekbar_progress"
    android:thumb="@drawable/custom_thumb" />

<强> custom_seekbar_progress.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="@drawable/seekbar_brown_to_show_progress" />
    </item>
</layer-list>

custom_thumb.xml 与您的相同

最后android:splitTrack="false"将删除拇指中不需要的空白

让我们来看看输出:

enter image description here

答案 3 :(得分:7)

首先礼貌地去 @Charuka

DO

您可以使用 android:progressDrawable="@drawable/seekbar" 代替 android:background="@drawable/seekbar"

progressDrawable 用于进度模式。

你应该试试

android:minHeight

  

定义视图的最小高度。视野无法保证   将能够达到这个最小高度(例如,如果它的   父布局用较小的可用高度约束它。)

<强> android:minWidth

  

定义视图的最小宽度。视野无法保证   将能够达到这个最小宽度(例如,如果它的父级   布局用较小的可用宽度约束它

    android:minHeight="25p"
    android:maxHeight="25dp" 

<强>供参考:

使用 android:minHeight和android:maxHeight 不是很好的解决方案。需要纠正你的自定义搜索栏(来自班级)

答案 4 :(得分:6)

Android自定义SeekBar - 自定义跟踪或进度,形状,大小,背景和拇指以及其他搜索栏自定义,请参阅http://www.zoftino.com/android-seekbar-and-custom-seekbar-examples

自定义跟踪可绘制

<?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:gravity="center_vertical|fill_horizontal">
        <shape android:shape="rectangle"
            android:tint="#ffd600">
            <corners android:radius="8dp"/>
            <size android:height="30dp" />
            <solid android:color="#ffd600" />
        </shape>
    </item>
    <item android:id="@android:id/progress"
        android:gravity="center_vertical|fill_horizontal">
        <scale android:scaleWidth="100%">
            <selector>
                <item android:state_enabled="false"
                    android:drawable="@android:color/transparent" />
                <item>
                    <shape android:shape="rectangle"
                        android:tint="#f50057">
                        <corners android:radius="8dp"/>
                        <size android:height="30dp" />
                        <solid android:color="#f50057" />
                    </shape>
                </item>
            </selector>
        </scale>
    </item>
</layer-list>

自定义拇指抽屉

?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:thickness="4dp"
    android:useLevel="false"
    android:tint="#ad1457">
    <solid
        android:color="#ad1457" />
    <size
        android:width="32dp"
        android:height="32dp" />
</shape>

<强>输出

enter image description here

答案 5 :(得分:1)

使用色调;)

<SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="15dp"
        android:minWidth="15dp"
        android:maxHeight="15dp"
        android:maxWidth="15dp"
        android:progress="20"
        android:thumbTint="@color/colorPrimaryDark"
        android:progressTint="@color/colorPrimary"/>

在thumbTint和progressTint中使用您需要的颜色。 它快得多! :)

编辑你可以与android:progressDrawable="@drawable/seekbar"

结合使用

答案 6 :(得分:1)

供将来的读者使用!

material-components-android 1.2.0-alpha01开始,您可以使用新的slider组件

例如:

相应地修改thumbSizethumbColortrackColor

<com.google.android.material.slider.Slider
        android:id="@+id/slider"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:valueFrom="20f"
        android:valueTo="70f"
        android:stepSize="10"
        app:thumbRadius="20dp"
        app:thumbColor="@color/colorAccent"
        app:trackColor="@android:color/darker_gray"
        />

注意:   轨道角不是圆的。

答案 7 :(得分:1)

您可以在材料组件库中使用Slider
使用这些属性来自定义颜色。

  • app:activeTrackColor:活动轨道的颜色
  • app:inactiveTrackColor:无效的轨道颜色
  • app:thumbColor:填补拇指

类似的东西:

    <com.google.android.material.slider.Slider
        android:id="@+id/slider"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:activeTrackColor="#ffd400"
        app:inactiveTrackColor="#e7e7e7"
        app:thumbColor="#ffb300"
        app:floatingLabel="false"
        .../>

enter image description here

它需要库的版本 1.2.0

答案 8 :(得分:0)

您可以尝试进度条而不是搜索栏

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_marginBottom="35dp"
    />

答案 9 :(得分:0)

android:minHeight android:maxHeight 对此非常重要。

<SeekBar
    android:id="@+id/pb"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxHeight="2dp"
    android:minHeight="2dp"
    android:progressDrawable="@drawable/seekbar_bg"
    android:thumb="@drawable/seekbar_thumb"
    android:max="100"
    android:progress="50"/>

<强> seekbar_bg.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">
        <shape>
            <corners android:radius="3dp" />
            <solid android:color="#ECF0F1" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="3dp" />
                <solid android:color="#C6CACE" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="3dp" />
                <solid android:color="#16BC5C" />
            </shape>
        </clip>
    </item>
</layer-list>

<强> seekbar_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#16BC5C" />
    <stroke
        android:width="1dp"
        android:color="#16BC5C" />
    <size
        android:height="20dp"
        android:width="20dp" />
</shape>

答案 10 :(得分:0)

改变条形和拇指颜色的非常简单的方法:

SeekBar slider;
ColorStateList sl = ColorStateList.valueOf( Color.YELLOW );
slider.setThumbTintList(sl);
slider.setProgressTintList(sl);
相关问题