Android 搜索栏,将路径颜色从黄色更改为白色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3480456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:23:29  来源:igfitidea点击:

Seek bar, change path color from yellow to white

androidandroid-emulator

提问by David Prun

I have two questions:

我有两个问题:

1) how do I change the color of the seek bar (path) from yellow (the default color) to white. What I mean to say is, while I slide the thumb , it turns the line traversed from grey to yellow. I want track/line to either remain grey or white..Basically I want just the thumb to move with no color change in the seek bar.

1)如何将搜索栏(路径)的颜色从黄色(默认颜色)更改为白色。我的意思是,当我滑动拇指时,它会将穿过的线从灰色变为黄色。我希望轨道/线保持灰色或白色..基本上我只希望拇指移动而搜索栏中没有颜色变化。

2) How to change the thumb of seekbar from rectangle to circle/sphere/round shape.

2) 如何将搜索栏的拇指从矩形更改为圆形/球形/圆形。

any pointers will be appreciated.

任何指针将不胜感激。

回答by Arnold

I want to complete the answer from above for the people who are new to the system,

我想为刚接触系统的人完成上面的回答,

the missing xmls ( background_fill , progress_fill and progress could look like that for a gradient red

缺少的 xmls( background_fill 、 progress_fill 和 progress 可能看起来像渐变红色

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>

background_fill.xml

background_fill.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="#FF555555" 
        android:centerColor="#FF555555"
        android:endColor="#FF555555" 
        android:angle="90" />

    <corners android:radius="5px" />

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

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

progress_fill.xml

progress_fill.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="#FF470000" 
        android:centerColor="#FFB80000"
        android:endColor="#FFFF4400" 
        android:angle="180" />

    <corners android:radius="5px" />

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

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

i did not complete the implementing for android:thumb, so the thumb will be still the original one

我没有完成android:thumb的实现,所以thumb还是原来的

Therefore we just have to delete this line again from our layout xml where we define the seekbar

因此,我们只需要从我们定义搜索栏的布局 xml 中再次删除这一行

android:thumb="@drawable/thumb"

Good luck!!!

祝你好运!!!

回答by Asahi

You should set SeekBar XML properties:

您应该设置 SeekBar XML 属性:

<SeekBar 
            ....
    android:progressDrawable="@drawable/progress"
    android:thumb="@drawable/thumb"
            ....
/>

Where progress is something like this:

进展是这样的:

<?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> 

and thumb is

拇指是

<?xml version="1.0" encoding="utf-8"?>    
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/thumb_fill" />
</selector>

回答by hedisam

Since SeekBaruses colorAccentby default, you can create a new style with your custom color as colorAccentthen use themeattribute to apply it to the SeekBar.

由于默认SeekBar使用colorAccent,您可以使用自定义颜色创建新样式,colorAccent然后使用theme属性将其应用于 SeekBar。

<SeekBar>
    .
    .
    android:theme="@style/MySeekBarTheme"
    .
</SeekBar>

in the @style:

在@style 中:

 <style name="MySeekBarTheme" parent="Widget.AppCompat.SeekBar" >
        <item name="colorAccent">#ffff00</item>
 </style>

回答by Alireza Ghanbarinia

seekBar.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
seekBar.getThumb().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);

回答by quent

You can just add tint from API level 21.

您可以从 API 级别 21 添加色调。

Add these properties to seekbar element:

将这些属性添加到 seekbar 元素:

android:progressTint="@android:color/white"android:thumbTint="@android:color/white"

android:progressTint="@android:color/white" android:thumbTint="@android:color/white"

Final result:

最后结果:

<SeekBar
    android:id="@+id/is"
    android:layout_width="match_parent"
    android:max="100"
    android:progressTint="@android:color/white"
    android:thumbTint="@android:color/white"/>

回答by Abhishek Sengupta

1. Create a drawable XML:Name it : progress_drawable

1. 创建一个可绘制的 XML:将其命名为:progress_drawable

<?xml version="1.0" encoding="UTF-8"?>

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

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

</shape>
<?xml version="1.0" encoding="UTF-8"?>

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

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

</shape>

2. Assign it to Seekbar

2.分配给Seekbar

            <SeekBar
                    android:id="@+id/slider_1"
                    android:progressDrawable="@drawable/progress_drawable"
                    android:layout_width="180dp"
                    android:layout_height="32dp"
                    android:layout_gravity="center"
                    android:rotation="270" />

回答by RexSplode

Just change the accent color of your style

只需更改您的风格的强调色

<style name="TickMarkSeekBar" parent="Theme.KefTheme">
        <item name="tickMark">@drawable/tickmark</item>
        <item name="thumbTint">@android:color/white</item>
        <item name="colorAccent">@android:color/white</item>
    </style>