2013-03-23 40 views
54

Tôi muốn thực hiện ProgressDialog như thế này, mà không cần khung thêm .: PicProgressDialog quay vòng tròn

Nhưng tôi nhận được thế này. Làm thế nào tôi có thể thay đổi điều đó?

enter image description here

Đây là mã của tôi cho ProgressDialog. Cảm ơn trước

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(); 
      } 
     } 
+0

Bạn có thể thử này để tạo các hộp thoại tùy chỉnh http://stackoverflow.com/a/26821095/1554031 – Satish

+0

Câu trả lời này phù hợp với tôi http://stackoverflow.com/questions/21957263/why-the-background-of-progressdialog-doesnt-set-to-the -transparent –

Trả lời

133

Chỉ cần thay đổi từ ProgressDialog để ProgressBar trong một bố cục:

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 Đây là một phong tục ProgressBar mà tôi sử dụng để thay đổi màu sắc mặc định.

<?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> 
+0

cảm ơn bạn đã trả lời. Tôi nên đặt gì vào thư mục có thể kéo? (tệp tiến trình nào?) –

+0

vui lòng xem các chỉnh sửa :) – JackTurky

+8

giá trị mã màu cho color_preloader_center, color_preloader_end và color_preloader_start là gì? –

16

Đặt XML này để chỉ hiển thị các bánh xe:

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

Tôi đã sử dụng View.INVISIBLE và View.VISIBLE và ProgressBar sẽ chậm đèn flash thay vì liên tục có thể nhìn thấy, chuyển sang View.GONE và View.VISIBLE và nó hoạt động hoàn hảo

-6

Hãy thử điều này .. .......

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

để bỏ ....

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