在ProgressBar Android中显示默认背景

在ProgressBar Android中显示默认背景,android,android-progressbar,Android,Android Progressbar,我想在ProgressBar中设置默认背景,这与我们以编程方式设置的背景不同。 对于下图所示的检查图像,其进度非常小,因此用户无法看到写入其上的进度数据。所以我希望在每个进度条后面有默认的任何颜色背景,就像下面显示的图像一样。 代码: adapter = new SimpleAdapter(this, aaReportData, R.layout.report_card1, new String[] { "Topic", "Accuracy",

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

对于下图所示的检查图像,其进度非常小,因此用户无法看到写入其上的进度数据。所以我希望在每个进度条后面有默认的任何颜色背景,就像下面显示的图像一样。

代码:

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);
adapter=new simpledapter(这是,
R.layout.report_card1,新字符串[]{“主题”,“准确性”,
“Accuracy1”},新int[]{R.id.tvTopic,
R.id.pbtopicaccurity,R.id.tvaccurity});
adapter.setViewBinder(新的SimpleAdapter.ViewBinder(){
@凌驾
公共布尔值setViewValue(视图、对象数据、,
字符串(文本表示法){
if(view.getId()==R.id.pbTopicAccuracy){
int value=Integer.parseInt(data.toString());
((进度条)视图)。设置进度(值);
RelativeLayout.LayoutParams params=新的RelativeLayout.LayoutParams(
价值,30);
params.leftMargin=185;
参数topMargin=3;
((ProgressBar)视图).setLayoutParams(params);

如果(值>=0&&value 10&&value 20&&value 30&&value 40&&value 50&&value 60&&value 70&&value 80&&value 90&&value请尝试下面这种绘图方式

 <?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>
然后像这样设置进度条

    <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:progressDrawable=“@drawable/ic_launcher”

或者使用自定义的
@drawable

在drawable文件夹中创建my_progress.xml,并使用类似
#000000
的颜色代码

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- background line-->
  <item android:id="@android:id/background">
      <shape>
          <gradient
              android:startColor="" 
              android:centerColor="" 
              android:centerY="1.0" 
              android:endColor="" 
              android:angle="270" 
              />
      </shape>
  </item>

  <!-- status line -->
  <item android:id="@android:id/progress">
      <clip>
          <shape>
              <gradient
                  android:startColor="" 
                  android:centerColor="" 
                  android:centerY="1.0" 
                  android:endColor="" 
                  android:angle="270" 
                  />
          </shape>
      </clip>
  </item>
</layer-list>

在布局xml中,您需要键入:

<ProgressBar
        android:layout_width="385dp" 
        android:layout_height="160dp" 
        style="android:style/Widget.ProgressBar.Horizontal" 
        android:id="+id/progressBar" 
        android:progressDrawable="@drawable/my_progress" 
        android:background="@drawable/ic_launcher" 
        android:max="100" 
        android:layout_gravity="center"/>


所以您正在动态添加此进度条。是的,实际上它是自定义listview的一部分。进度条工作正常。我将作为第一个图像获得结果。现在,我想设置与setProgress不同的默认背景。您可以在第二个图像中看到它。自定义进度可绘制有什么问题?@pskink,这不是问题正确阅读我的帖子。
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- background line-->
  <item android:id="@android:id/background">
      <shape>
          <gradient
              android:startColor="" 
              android:centerColor="" 
              android:centerY="1.0" 
              android:endColor="" 
              android:angle="270" 
              />
      </shape>
  </item>

  <!-- status line -->
  <item android:id="@android:id/progress">
      <clip>
          <shape>
              <gradient
                  android:startColor="" 
                  android:centerColor="" 
                  android:centerY="1.0" 
                  android:endColor="" 
                  android:angle="270" 
                  />
          </shape>
      </clip>
  </item>
</layer-list>
<ProgressBar
        android:layout_width="385dp" 
        android:layout_height="160dp" 
        style="android:style/Widget.ProgressBar.Horizontal" 
        android:id="+id/progressBar" 
        android:progressDrawable="@drawable/my_progress" 
        android:background="@drawable/ic_launcher" 
        android:max="100" 
        android:layout_gravity="center"/>