setProgressDrawable fills the entire seekBar

2019-06-18 16:13发布

问题:

As I say in the title, when I use setProgressDrawable, it fills the entire SeekBar: if progress is at 34%, progress show 100% but thumb shows the correct percentatge 34%. I don't figure out what can be the problem...

   done.setProgressDrawable(getResources().getDrawable(R.drawable.seekbar_progress));
   done.setThumb(getResources().getDrawable(R.drawable.seekbar_thumb));

seekbar_progress.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="15dp"    android:color="#52a8ec"/>

seekbar_thumb.xml

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
<gradient android:startColor="#ffffffff" android:endColor="#ff585858" android:angle="270"/>
<size android:height="17dp" android:width="5dp"/>
</shape>

Any idea?

Thank you in advance!

回答1:

try to put the fill shape inside a clip tag like so

<clip>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="15dp"    android:color="#52a8ec"/>
</clip>


回答2:

I faced the same problem and the following code resolved it.

Wherever you are using the code to change your drawable just do the following (where 'p' is your seekbar) :

int currentProgress = p.getProgress();
p.setProgress(0);
Rect bounds = p.getProgressDrawable().getBounds();
p.setProgressDrawable(getResources().getDrawable(R.drawable.progress_drawable_orange));
p.getProgressDrawable().setBounds(bounds);
p.setProgress(currentProgress);

and do not forget to add the following check in your onProgressChanged method:

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    if(!fromUser)
        return;
    // ... THE REST OF YOUR CODE
}

example



回答3:

I ended in the same situation as you did and managed to resolve the problem by implementing progress with layer-list XML file. See my answer here.



回答4:

You can tweak the progressTint field in your layout xml to change the progress color without affecting the SeekBar's entire background.

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:progressTint="@color/seek_bar_progress"/>

There's also thumbTint and progressBackgroundTint.