gpt4 book ai didi

android - 在 ProgressBar Android 中显示默认背景

转载 作者:行者123 更新时间:2023-11-30 03:01:51 30 4
gpt4 key购买 nike

我想在 ProgressBar 中设置默认背景,这与我们以编程方式设置的背景不同。

对于如下所示的检查图像,它的进度非常少,因此用户无法看到写在上面的进度数据。所以我想在每个进度条后面默认任何颜色背景,如下图所示。
progress bar

enter image description here

代码:

adapter = new SimpleAdapter(this, aaReportData,
R.layout.report_card1, new String[] { "Topic", "Accuracy",
"Accuracy1" }, new int[] { R.id.tvTopic,
R.id.pbTopicAccuracy, R.id.tvAccuracy });

adapter.setViewBinder(new SimpleAdapter.ViewBinder() {

@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
if (view.getId() == R.id.pbTopicAccuracy) {
int value = Integer.parseInt(data.toString());
((ProgressBar) view).setProgress(value);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
value, 30);
params.leftMargin = 185;
params.topMargin = 3;
((ProgressBar) view).setLayoutParams(params);

if (value >= 0 && value <= 10) {
((ProgressBar) view)
.setProgressDrawable(getResources()
.getDrawable(
R.drawable.progress_010));
}
if (value > 10 && value <= 20) {
((ProgressBar) view)
.setProgressDrawable(getResources()
.getDrawable(
R.drawable.progress_1020));
}
if (value > 20 && value <= 30) {
((ProgressBar) view)
.setProgressDrawable(getResources()
.getDrawable(
R.drawable.progress_2030));
} else if (value > 30 && value <= 40) {
((ProgressBar) view)
.setProgressDrawable(getResources()
.getDrawable(
R.drawable.progress_3040));
} else if (value > 40 && value <= 50) {
((ProgressBar) view)
.setProgressDrawable(getResources()
.getDrawable(
R.drawable.progress_4050));
} else if (value > 50 && value <= 60) {
((ProgressBar) view)
.setProgressDrawable(getResources()
.getDrawable(
R.drawable.progress_5060));
} else if (value > 60 && value <= 70) {
((ProgressBar) view)
.setProgressDrawable(getResources()
.getDrawable(
R.drawable.progress_6070));
} else if (value > 70 && value <= 80) {
((ProgressBar) view)
.setProgressDrawable(getResources()
.getDrawable(
R.drawable.progress_7080));
} else if (value > 80 && value <= 90) {
((ProgressBar) view)
.setProgressDrawable(getResources()
.getDrawable(
R.drawable.progress_8090));
} else if (value > 90 && value <= 100) {
((ProgressBar) view)
.setProgressDrawable(getResources()
.getDrawable(
R.drawable.progress_90100));
}

return true;
}

return false;
}
});

lvReportData.setAdapter(adapter);

可绘制: progress_010

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

<gradient
android:angle="90"
android:endColor="#A82222"
android:startColor="#8E0000"
android:type="linear" />

<corners
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp" />

</shape>

最佳答案

像下面这样尝试这个 Drawable

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

<!-- Define background color gradient -->
<item android:id="@android:id/background">
<shape>
<gradient
android:angle="90"
android:centerColor="#cccccc"
android:centerY="1.0"
android:endColor="#bbbbbb"
android:startColor="#dddddd" />
</shape>
</item>

<!-- Define progress bar color gradient -->
<item android:id="@android:id/progress">
<clip>
<shape>
<gradient
android:angle="270"
android:centerColor="#b4ad9e"
android:centerY="1.0"
android:endColor="#867961"
android:startColor="#e6d6bd" />
</shape>
</clip>
</item>

同时创建主题 res/values/styles.xml

 <style name="ProgressBar.Horizontal.Indeterminate" parent="@android:style/Widget.Holo.Light.ProgressBar.Horizontal">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:indeterminate">true</item>
</style>

然后像这样设置你的进度条

    <ProgressBar
android:id="@+id/progress_bar"
style="@style/ProgressBar.Horizontal.Indeterminate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="30dp" />

告诉我。当然,您可以根据需要更改渐变颜色。

关于android - 在 ProgressBar Android 中显示默认背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22402159/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com