简体   繁体   English

以编程方式更改seekbar的进度绘制

[英]Changing Progress Drawable of seekbar programmatically

In my app I have a seek bar which is supposed to have two states - focused and normal在我的应用程序中,我有一个搜索栏,它应该有两种状态 - 聚焦和正常

Nor for progress drawable I have set the normal image in xml like this -对于可绘制的进度,我也像这样在 xml 中设置了普通图像 -

<SeekBar
    android:id="@+id/bar"
    android:layout_width="315dp"
    android:layout_height="10dp"
    android:layout_marginLeft="114dp"
    android:layout_marginTop="210dp"
    android:max="60"
    android:progressDrawable="@drawable/bar_normal"
    android:thumb="@null" />

And here is the xml I use for progress drawable这是我用于进度绘制的 xml

bar_normal.xml bar_normal.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/black"/>

    <item android:id="@android:id/secondaryProgress">
        <clip android:drawable="@drawable/black" />
    </item>

    <item android:id="@android:id/progress">
        <clip android:drawable="@drawable/grey" />
    </item>

</layer-list>

Now when user touches this seekbar I want to change the progress drawable to become orange现在,当用户触摸此搜索栏时,我想将可绘制进度更改为橙色

for this I use another xml and in my code I write like this为此,我使用另一个 xml,在我的代码中我这样写

seekBar.setProgressDrawable(getResources().getDrawable(R.drawable.bar_focused));

bar_focused.xml bar_focused.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/black"/>

    <item android:id="@android:id/secondaryProgress">
        <clip android:drawable="@drawable/black" />
    </item>

    <item android:id="@android:id/progress">
        <clip android:drawable="@drawable/orange" />
    </item>

</layer-list>

Initially the bar appears just fine, with progress shown in grey color and background and secondary progress in black color, but when I touch the seekbar to make it orange, the whole bar becomes black.最初栏看起来很好,进度以灰色和背景显示,次要进度以黑色显示,但是当我触摸搜索栏使其变为橙色时,整个栏变成黑色。 Its working remains fine but I can not see orange color.它的工作仍然很好,但我看不到橙色。

you must not change the drawable using the state, let the Os do it for you, the android default seekbar drawable is:您不能使用状态更改 drawable,让 Os 为您完成,android 默认的 seekbar drawable 是:

    <item android:state_pressed="true"
          android:state_window_focused="true"
          android:drawable="@drawable/seek_thumb_pressed" />

    <item android:state_focused="true"
          android:state_window_focused="true"
          android:drawable="@drawable/seek_thumb_selected" />

    <item android:state_selected="true"
          android:state_window_focused="true"
          android:drawable="@drawable/seek_thumb_selected" />

    <item android:drawable="@drawable/seek_thumb_normal" />

</selector>

Ps: there is no difference if you drawable are image or defined in xml for your case it should be (bar_drawable.xml): Ps:如果您的drawable是图像或在xml中定义,则没有区别,应该是(bar_drawable.xml):

<item android:state_pressed="true"
      android:state_window_focused="true"
      android:drawable="@drawable/bar_focused" />

<item android:state_focused="true"
      android:state_window_focused="true"
      android:drawable="@drawable/bar_focused" />

<item android:state_selected="true"
      android:state_window_focused="true"
      android:drawable="@drawable/bar_focused" />

<item android:drawable="@drawable/bar_normal" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM