📜  自定义progressdialog android (1)

📅  最后修改于: 2023-12-03 15:27:43.838000             🧑  作者: Mango

自定义ProgressDialog Android

ProgressDialog is a class that displays a dialog with a spinning progress wheel and a message to the user. It is a built-in class in Android, but it may not always match the style of your application. If you want to customize the ProgressDialog, you can create your own custom layout and use it to display your progress.

Step 1: Create a Custom Layout

The first thing you need to do is create a custom layout for your ProgressDialog. You can create the layout using XML in the same way you create any other Android layout.

Here is an example layout for a custom ProgressDialog:

<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="20dp">

   <ProgressBar
      android:id="@+id/progress"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"/>

   <TextView
      android:id="@+id/message"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/progress"
      android:textColor="@android:color/white"
      android:text="Loading..."
      android:layout_marginTop="10dp"
      android:gravity="center_horizontal"/>

</RelativeLayout>

This layout includes a ProgressBar to show the progress and a TextView to display a message to the user.

Step 2: Create a Custom Class

Next, you need to create a custom class that extends the ProgressDialog class. In your custom class, you can set the layout you created in the previous step as the content view for the ProgressDialog.

Here is an example custom class:

public class CustomProgressDialog extends ProgressDialog {

   public CustomProgressDialog(Context context) {
      super(context);
   }

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.custom_progress_dialog);
   }

}

In the constructor, you call the super constructor with the context parameter. In the onCreate method, you set the content view to the layout you created in the previous step.

Step 3: Use Custom ProgressDialog

Now that you have created your custom ProgressDialog, you can use it in your application. To use it, simply create an instance of your custom class and call the show() method:

CustomProgressDialog progressDialog = new CustomProgressDialog(this);
progressDialog.show();

You can also customize the message displayed to the user by accessing the TextView in your custom layout:

TextView message = progressDialog.findViewById(R.id.message);
message.setText("Custom message");
Conclusion

Customizing the ProgressDialog in Android is easy. By creating your own custom layout and class, you can create a ProgressDialog that matches the style of your application and provides a better user experience to your users.