ProgressBar在运行时使用白色背景,如何从ProgressBar中删除白色背景


   private static ProgressDialog pDialog; pDialog = new ProgressDialog(activity); 
pDialog.setIndeterminate(true); 
pDialog.setCancelable(false); 
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:indeterminate="false" />

您可以自定义进度条。自定义进度条需要为进度条的背景和进度定义一个或多个属性。

在res->drawable文件夹中创建一个名为customprogressbar.XML的XML文件:

custom_progressbar.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Define the background properties like color etc -->
    <item android:id="@android:id/background">
    <shape>
        <gradient
                android:startColor="#000001"
                android:centerColor="#0b131e"
                android:centerY="1.0"
                android:endColor="#0d1522"
                android:angle="270"
        />
    </shape>
   </item>
  <!-- Define the progress properties like start color, end color etc -->
  <item android:id="@android:id/progress">
    <clip>
        <shape>
            <gradient
                android:startColor="#007A00"
                android:centerColor="#007A00"
                android:centerY="1.0"
                android:endColor="#06101d"
                android:angle="270"
            />
        </shape>
    </clip>
    </item>
</layer-list> 

现在您需要在customprogressbar.xml(drawable)中设置progressDrawable属性

您可以在XML文件或"活动"(在运行时)中执行此操作。

在XML中执行以下操作:

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:progressDrawable="@drawable/custom_progressbar"         
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
At run time do the following
// Get the Drawable custom_progressbar                     
    Drawable draw=res.getDrawable(R.drawable.custom_progressbar);
// set the drawable as progress drawable
    progressBar.setProgressDrawable(draw);

这个问题毫无意义。在这个问题中,您创建了一个ProgressDialog,它实际上是Dialog的派生。对话框始终具有基于应用程序主题的白色/深色背景。

如果您只想在活动中显示进度条,

添加

<ProgressBar  android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/pgBar"
android:indeterminate="true" /> 

在活动/片段的布局中

然后像这个一样访问进度条

ProgressBar pgBar = (ProgressBar) findViewById(R.id.pgBar);
pgBar.setIndeterminate(true);
pgBar.setVisibility(View.Visible)

进度条的默认回退为白色。我们可以使用更改进度条的颜色

  1. android:progressDrawable在xml或
  2. 用程序设置ProgressDrawable(drawable)方法

可绘制文件可以是直接插入可绘制文件夹的图像,也可以在可绘制文件夹中创建xml文件。xml可绘制文件是首选,因为它比图像更优化。

ProgressBar progessBar = new ProgressBar(MainActivity.this);
        Drawable dr = getResources().getDrawable(R.drawable.pink);
        pDialog.setProgressDrawable(dr);

其中pink.xl是描述进度条的背景的xml文件

最新更新