在ProgressBar Android中显示默认背景

时间:2014-03-14 10:18:48

标签: android android-progressbar

我想在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>

2 个答案:

答案 0 :(得分:1)

尝试下面的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" />

让我知道。当然,您可以根据需要更改渐变颜色。

答案 1 :(得分:0)

您可以使用参数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"/>
相关问题