【问题标题】:ProgressDialog spinning circleProgressDialog 旋转圈
【发布时间】:2013-03-13 05:15:28
【问题描述】:

我想像这样实现 ProgressDialog,无需额外的框架。:

但我得到了这个。我怎么能改变呢?

这是我的 ProgressDialog 代码。 提前致谢

private ProgressDialog mProgressDialog;
   ............
   mProgressDialog = new ProgressDialog(activity);
   mProgressDialog.setIndeterminate(false);
   mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
   ............
    public class ProgressTask extends AsyncTask <Fragment,Void,Fragment>{
            @Override
            protected void onPreExecute(){
                mProgressDialog.show();
            }

            @Override
            protected Fragment doInBackground(Fragment... arg0) {   
                    //my stuff is here
            }

            @Override
            protected void onPostExecute(Fragment result) {
                   mProgressDialog.dismiss();
            }
        }

【问题讨论】:

标签: android android-layout android-progressbar


【解决方案1】:

只需将布局中的ProgressDialog 更改为ProgressBar

res/layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        //Your content here
     </LinearLayout>

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:visibility="gone"
            android:indeterminateDrawable="@drawable/progress" >
        </ProgressBar>
</RelativeLayout>

src/yourPackage/YourActivity.java

public class YourActivity extends Activity{

private ProgressBar bar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout);
    bar = (ProgressBar) this.findViewById(R.id.progressBar);
    new ProgressTask().execute();
}
private class ProgressTask extends AsyncTask <Void,Void,Void>{
    @Override
    protected void onPreExecute(){
        bar.setVisibility(View.VISIBLE);
    }

    @Override
    protected Void doInBackground(Void... arg0) {   
           //my stuff is here
    }

    @Override
    protected void onPostExecute(Void result) {
          bar.setVisibility(View.GONE);
    }
}
}

drawable/progress.xml 这是我用来更改默认颜色的自定义ProgressBar

<?xml version="1.0" encoding="utf-8"?>

<!-- 
    Duration = 1 means that one rotation will be done in 1 second. leave it.
    If you want to speed up the rotation, increase duration value. 
    in example 1080 shows three times faster revolution. 
    make the value multiply of 360, or the ring animates clunky 
-->
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="1"
    android:toDegrees="360" >

    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="8"
        android:useLevel="false" >
        <size
            android:height="48dip"
            android:width="48dip" />

        <gradient
            android:centerColor="@color/color_preloader_center"
            android:centerY="0.50"
            android:endColor="@color/color_preloader_end"
            android:startColor="@color/color_preloader_start"
            android:type="sweep"
            android:useLevel="false" />
    </shape>

</rotate>

【讨论】:

  • 感谢您的回答。我应该把什么放到drawable文件夹中? (进度文件是什么?)
  • color_preloader_center、color_preloader_end 和 color_preloader_start 的颜色代码值是多少?
  • 感谢这个答案:) 有没有办法让它旋转得更快?
  • @SankarV 我用过 #000000#000000#ff56a9c7
  • 我改变了drawable/progress.xml中高度和宽度的size标签的值,但是没有任何效果。有什么想法吗?
【解决方案2】:

把这个XML只显示轮子:

<ProgressBar
    android:indeterminate="true"
    android:id="@+id/marker_progress"
    style="?android:attr/progressBarStyle"
    android:layout_height="50dp" />

【讨论】:

    【解决方案3】:

    我使用的是View.INVISIBLEView.VISIBLEProgressBar会慢慢闪烁而不是一直可见,切换到View.GONEView.VISIBLE,效果很好

    【讨论】:

      【解决方案4】:

      ProgressBar Dialog - 无需一直编写自定义progressBar 代码,创建一个下面的类并使用

      public class DProgressbar {
          Dialog progressDialog ;
          public DProgressbar(Context context) {
              progressDialog = new Dialog(context);
              progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
              progressDialog.setContentView(R.layout.dialog_api_loading);
              progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
              progressDialog.setCancelable(false);
          }
      
          public void show(){
              progressDialog.show();
          }
      
          public void dismiss(){
              progressDialog.dismiss();
          }
      
      }
      

      R.layout.dialog_api_loading

        <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:cardCornerRadius="@dimen/size_10">
      
          <LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@color/white"
              android:orientation="horizontal"
              android:padding="20dp">
      <ProgressBar
                  android:layout_width="@dimen/size_20"
                  android:layout_height="@dimen/size_20" />
                  <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:layout_marginHorizontal="@dimen/txt_distance_5"
                  android:gravity="center"
                  android:text="Loading..." />
          </LinearLayout>
      </androidx.cardview.widget.CardView>
      

      【讨论】:

      • 不鼓励仅使用代码回答。请提供您的答案如何解决问题的摘要,以及为什么它可能比提供的其他答案更可取。
      【解决方案5】:

      试试这个…………

      ProgressDialog pd1;
      pd1=new ProgressDialog(<current context reference here>);
      pd1.setMessage("Loading....");
      pd1.setCancelable(false);
      pd1.show();
      

      解雇....

      if(pd1!=null)
       pd1.dismiss(); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多