【问题标题】:Changing Progress Drawable of seekbar programmatically以编程方式更改搜索栏的进度绘图
【发布时间】:2012-09-26 09:30:34
【问题描述】:

在我的应用中,我有一个搜索栏,它应该有两种状态 - 聚焦和正常

我也没有像这样在 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" />

这是我用于进度可绘制的 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>

现在,当用户触摸此搜索栏时,我想将可绘制的进度更改为橙色

为此,我使用另一个 xml,并在我的代码中这样编写

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

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>

最初该栏看起来很好,进度显示为灰色和背景,第二进度显示为黑色,但是当我触摸搜索栏使其变为橙色时, 整个条变黑。它的工作仍然很好,但我看不到橙色。

【问题讨论】:

    标签: android seekbar


    【解决方案1】:

    您不能使用状态更改可绘制对象,让操作系统为您完成, 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:如果您的可绘制对象是图像或在 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" />
    

    【讨论】:

      猜你喜欢
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 2019-07-03
      • 2013-10-08
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      相关资源
      最近更新 更多