如何设置Android的搜索栏进度绘制编程 [英] How to set Android SeekBar progress drawable programatically

查看:110
本文介绍了如何设置Android的搜索栏进度绘制编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有编程设置搜索栏的进步绘制一个问题。 当我在的.xml设置文件,一切工作正常。

I have a problem with programatically setting the progress drawable of a SeekBar. When I set it in the .xml file everything is working fine.

<SeekBar
 android:id="@+id/sb"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 .....
 android:progressDrawable="@drawable/seek_bar"/>

不过,我有一个问题,当我尝试从code是这样设置的:

But, I have a problem when I try to set it from code like this:

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

背景绘制然后把整个搜索栏,我不能够修改进度在所有以后 - 拇指移动,但进展仍然绘制填补整个搜索栏。此外,搜索栏失去其圆角。如此看来,进步绘制是在搜索栏的顶部。

Background drawable then takes the whole seek bar, and I'm not able to modify the progress at all later on - the thumb moves but the progress drawable still fills whole seekbar. Also, seekbar looses its rounded corners. It seems that progress drawable is on top of the seekbar.

我试过设置在<一个解决方案href="http://stackoverflow.com/questions/4348032/android-progressbar-does-not-update-progress-view-drawable">android进度不更新进度视图/绘制,但它不为我工作。

I tried the solution provided on android progressBar does not update progress view/drawable, but it doesn't work for me.

帮助AP preciated,

Help appreciated,

米洛斯

推荐答案

我用的.xml形状,背景为我的搜索栏解决了这个问题。 可通过setProgressDrawable可以使用完整的搜索栏的解决方案()方法应该是这样的:

I solved the problem by using .xml shape as background for my SeekBar. The complete SeekBar solution that can be used via setProgressDrawable() method should be like this:

//seek_bar.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/seek_bar_background"/>
<item android:id="@android:id/progress"
    android:drawable="@drawable/seek_bar_progress" />
</layer-list>

//seek_bar_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<gradient
    android:angle="270"
    android:startColor="#8a8c8f"
    android:endColor="#58595b" />

<corners android:radius="5dip" />

</shape>

//seek_bar_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/seek_bar_background"/>
<item android:id="@android:id/progress">
    <clip android:drawable="@drawable/seek_bar_progress_fill" />
</item>

</layer-list>

//seek_bar_progress_fill.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<gradient
    android:startColor="#b3d27e"       
    android:endColor="#93c044"
    android:angle="270"
 />
 <corners android:radius="5dip" />  

</shape>

在code,你可以用这样的:

In the code, you can use it like this:

progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.seek_bar));

这篇关于如何设置Android的搜索栏进度绘制编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆