在Android中间隔更改搜索栏颜色

时间:2017-06-09 13:41:03

标签: android android-seekbar

我希望在进度发生变化时更改android中的搜索栏颜色。在下图中,蓝色为1-2,绿色为2-3,依此类推。可能吗? 到目前为止,我的代码是;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar);
        seekBar.setProgress(0);
        seekBar.incrementProgressBy(10);
        seekBar.setMax(200);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                progress = progress / 10;
                progress = progress * 10;
                    int stepSize = 25;
                    progress = (progress/stepSize)*stepSize;
                    seekBar.setProgress(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

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


<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

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


In xml where You write seekabr code add this



android:thumb="@drawable/thumb"

I hope this code is help to solve Your Problem.
Do You have idea How can you add These Files in Your Project
相关问题