关于ProgressBar.setProgressDrawable()的问题

hedaogui 2011-10-28 02:54:47
我写了progressdrawable的xml:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/probardown">
<!-- 设置背景色图像资源 -->
<item android:id="@android:id/background" android:drawable="@drawable/grey" />
<!-- 设置第二级进度条颜色图像资源 -->
<item android:id="@android:id/secondaryProgress" android:drawable="@drawable/grey" />
<!-- 设置第一级进度条颜色图像资源 -->
<item android:id="@android:id/progress" android:drawable="@drawable/green" />
</layer-list>
如果直接在
<ProgressBar
android:id="@+id/myProgressBar03"
android:layout_width="300px"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/progressdrawable"
android:progress="0"
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
/>
这样显示的就是正常的,一级进度条为绿色,二级和进度条底色为灰色
如果用
Drawable progressDrawable=getResources().getDrawable(R.drawable.progressdrawable);
myProgressBar03.setProgressDrawable(progressDrawable)
这样就是不正常的,整个进度条是绿色的,无论增加进度还是减进度。
希望有人给我解答,谢谢先
...全文
1372 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hedaogui 2011-12-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ooooooobh 的回复:]
补充一点加载图片的时候可以加载9patch图片。。。。
例如:
<item android:id="@android:id/background" >
<nine-patch android:src="@drawable/background"> </nine-patch>
</item>
<item android:id="@android:id/progress">……
[/Quote]
多谢你的方法,等我下次做课设的时候试一下,毕竟图片样式更多也方便点
hedaogui 2011-12-04
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ooooooobh 的回复:]
你之前的方法也行,不过格式要和你之后的这个方法一样就行了。那样的话背景可以改为图片,而不单单只是颜色。
[/Quote]

恩,我知道性质不太一样,之前有用图片代替过颜色,但是那样出来的进度条不是圆角的,
云浩舟 2011-12-03
  • 打赏
  • 举报
回复
补充一点加载图片的时候可以加载9patch图片。。。。
例如:
<item android:id="@android:id/background" >
<nine-patch android:src="@drawable/background"> </nine-patch>
</item>
<item android:id="@android:id/progress">
<clip>
<nine-patch android:src="@drawable/progress"> </nine-patch>
</clip>
</item>
云浩舟 2011-12-03
  • 打赏
  • 举报
回复
你之前的方法也行,不过格式要和你之后的这个方法一样就行了。那样的话背景可以改为图片,而不单单只是颜色。
hedaogui 2011-10-28
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" >

<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip >
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ffffd300"
android:centerColor="#ffffb600"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>


这是我最后用的
源文件地址http://www.girlcoding.com/2011/04/custom-horizontal-progressbar/#comment-512
hedaogui 2011-10-28
  • 打赏
  • 举报
回复
问题解决了
hedaogui 2011-10-28
  • 打赏
  • 举报
回复
其他的java代码一样吗,感觉那几个item不太像用在progressbar上的,能问下你的是如何应用的,是用在什么什么控件上的,可以给我贴下关键的代码不
wc7253027 2011-10-28
  • 打赏
  • 举报
回复
在drawable 里面创建xml文件。和你用的layer-list 是差不多的东西吧。。。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#000" />
<item android:state_focused="true" android:color="#000" />
<item android:state_pressed="true" android:color="#000" />
<item android:color="#fff" />
</selector>


你改下试下。。应该是可以的
hedaogui 2011-10-28
  • 打赏
  • 举报
回复


[Quote=引用 1 楼 wc7253027 的回复:]
Java code


<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/probardown">
<!-- 设置背景色图像资源 -->
<item android:id="@android:id/background" android:dra……
[/Quote]

在哪里用<selector></selector>
wc7253027 2011-10-28
  • 打赏
  • 举报
回复

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/probardown">
<!-- 设置背景色图像资源 -->
<item android:id="@android:id/background" android:drawable="@drawable/grey" />
<!-- 设置第二级进度条颜色图像资源 -->
<item android:id="@android:id/secondaryProgress" android:drawable="@drawable/grey" />
<!-- 设置第一级进度条颜色图像资源 -->
<item android:id="@android:id/progress" android:drawable="@drawable/green" />
</layer-list>


是不是这个的原因?我用<selector></selector>就可以的。。汗。。。

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧