Seekbar进度可暂停异常行为onPause

时间:2016-08-09 18:51:15

标签: android android-drawable seekbar

我在片段中显示了一个搜索栏。搜索栏具有自定义拇指和进度绘图。它们显示正常,但是当我暂停活动并返回它时,进度条宽度奇怪地增加到拇指的相同高度!我找不到这种异常行为的问题。

它是如何正常显示的 enter image description here

活动停顿后

enter image description here

我的代码:

//片段onStart

seekBar.setThumb(getResources().getDrawable(R.drawable.form_bg));
            seekBar.setProgressDrawable(getResources().getDrawable(R.drawable.progress));

// seekBar xml

<SeekBar
                    android:id="@+id/seekBar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:indeterminate="false"
                    android:layout_marginTop="5dp"
                    android:max="100"
                    android:maxHeight="6dp"
                    android:progress="50" />

// thumb drawable

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <size android:height="@dimen/seekbar_tumb_size" android:width="@dimen/seekbar_tumb_size" />
    <solid android:color="@color/form_color" />
</shape>

// progress drawable

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

<item android:top="0dp" android:bottom="0dp" android:gravity="center_vertical"  android:id="@android:id/background">
    <shape>

        <solid android:color="@color/activity_bg" />
    </shape>
</item>

<item android:gravity="center_vertical" android:id="@android:id/progress">
    <clip>
        <shape>
            <solid android:color="@color/form_color_syrup" />
        </shape>
    </clip>
</item>

1 个答案:

答案 0 :(得分:0)

终于找到了解决方案。

Rect bounds = seekBar.getProgressDrawable().getBounds();

    seekBar.setThumb(getResources().getDrawable(R.drawable.form_bg));
    seekBar.setProgressDrawable(getResources().getDrawable(R.drawable.progress));

seekBar.getProgressDrawable().setBounds(bounds);