Android - 样式搜索栏

标签 android android-seekbar custom-selectors android-slider

我想设计一个搜索栏,如下图所示。

enter image description here

通过使用默认搜索栏,我会得到这样的结果:

enter image description here

所以我只需要改变颜色。我不需要额外的样式。是否有任何直接的方法可以做到这一点,或者我应该构建我的自定义可绘制对象。

我尝试构建自定义一个,但我无法获得如上所示的确切一个。 使用自定义drawable后,我得到如下图:

enter image description here

如果我需要构建自定义的,那么请建议如何减少进度线的宽度以及形状。

我的自定义实现:

background_fill.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:angle="90"
        android:centerColor="#FF555555"
        android:endColor="#FF555555"
        android:startColor="#FF555555" />

    <corners android:radius="1dp" />

    <stroke
        android:width="1dp"
        android:color="#50999999" />
    <stroke
        android:width="1dp"
        android:color="#70555555" />

</shape>

progress_fill.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:angle="90"
        android:centerColor="#FFB80000"
        android:endColor="#FFFF4400"
        android:startColor="#FF470000" />

    <corners android:radius="1dp" />

    <stroke
        android:width="1dp"
        android:color="#50999999" />
    <stroke
        android:width="1dp"
        android:color="#70555555" />

</shape>

progress.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/background_fill"/>
    <item android:id="@android:id/progress">
        <clip android:drawable="@drawable/progress_fill" />
    </item>

</layer-list>

thumb.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <gradient
        android:angle="270"
        android:endColor="#E5492A"
        android:startColor="#E5492A" />

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

</shape>

搜索栏:

<SeekBar
        android:id="@+id/seekBarDistance"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="88dp"
        android:progressDrawable="@drawable/progress"
        android:thumb="@drawable/thumb" >
    </SeekBar>

最佳答案

我将从 Android 源代码中提取可绘制对象和 xml,并将其颜色更改为红色。 这是我如何为 mdpi drawables 完成此操作的示例:

自定义red_scrubber_control.xml(添加到res/drawable):

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

    <item android:drawable="@drawable/red_scrubber_control_disabled_holo" android:state_enabled="false"/>
    <item android:drawable="@drawable/red_scrubber_control_pressed_holo" android:state_pressed="true"/>
    <item android:drawable="@drawable/red_scrubber_control_focused_holo" android:state_selected="true"/>
    <item android:drawable="@drawable/red_scrubber_control_normal_holo"/>
</selector>

自定义:red_scrubber_progress.xml

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

    <item
        android:id="@android:id/background"
        android:drawable="@drawable/red_scrubber_track_holo_light"/>
    <item android:id="@android:id/secondaryProgress">
        <scale
            android:drawable="@drawable/red_scrubber_secondary_holo"
            android:scaleWidth="100%" />
    </item>
    <item android:id="@android:id/progress">
        <scale
            android:drawable="@drawable/red_scrubber_primary_holo"
            android:scaleWidth="100%" />
    </item>

</layer-list>

然后从Android源代码中复制所需的drawables,我拿了from this link

最好为每个 hdpi、mdpi、xhdpi 复制这些可绘制对象。例如我只使用 mdpi:

然后使用 Photoshop 将颜色从蓝色变为红色:

red_scrubber_control_disabled_holo.png: red_scrubber_control_disabled_holo

red_scrubber_control_focused_holo.png: red_scrubber_control_focused_holo

red_scrubber_control_normal_holo.png: red_scrubber_control_normal_holo

red_scrubber_control_pressed_holo.png: red_scrubber_control_pressed_holo

red_scrubber_primary_holo.9.png: red_scrubber_primary_holo.9

red_scrubber_secondary_holo.9.png: red_scrubber_secondary_holo.9

red_scrubber_track_holo_light.9.png: red_scrubber_track_holo_light.9

将 SeekBar 添加到布局中:

<SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:progressDrawable="@drawable/red_scrubber_progress"
    android:thumb="@drawable/red_scrubber_control" />

结果:

enter image description here

关于Android - 样式搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16163215/

相关文章:

javascript - 如何选择仅具有特定属性的元素?

android - 将 ADT 更新到 22.0.1 后缺少 R.java

android - 如何为所有按钮添加一个通用监听器(无干扰)?

android - 在自定义 AlertDialog 中插入 SeekBar (Android)

android - SeekBar setMin 在 Android 中至少需要 api 26?

testing - 在页面模型中将参数传递给 TestCafe Selector

android - 使用 Facebook 登录按钮请求权限

android - android : setDrawingCacheEnabled throws a CalledFromWrongThreadException 上的编程屏幕截图

java - Android:多次使用setOnSeekBarChangeListner方法

jquery - 伪类选择器和自定义选择器的相对性能如何?