且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ProgressDialog 已弃用.要使用的替代方法是什么?

更新时间:2023-02-19 13:55:06

是的,在 API 级别 26 中它已被弃用.相反,您可以使用 progressBar.

Yes, in API level 26 it's deprecated. Instead, you can use progressBar.

以编程方式创建:

首先获取对根布局的引用

First get a reference to the root layout

RelativeLayout layout = findViewById(R.id.display);  //specify here Root layout Id

RelativeLayout layout = findViewById(this);

然后添加进度条

progressBar = new ProgressBar(youractivity.this, null, android.R.attr.progressBarStyleLarge);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar, params);

显示进度条

progressBar.setVisibility(View.VISIBLE);

隐藏进度条

progressBar.setVisibility(View.GONE);

要禁用用户交互,您只需添加以下内容代码

To disable the user interaction you just need to add the following code

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                           WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

要恢复用户交互,您只需添加以下代码

To get user interaction back you just need to add the following code

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

仅供将来参考,将 android.R.attr.progressBarStyleSmall 更改为 android.R.attr.progressBarStyleHorizo​​ntal.

Just for future reference, change the android.R.attr.progressBarStyleSmall to android.R.attr.progressBarStyleHorizontal.

以下代码仅适用于API level 21

progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));

通过 xml 创建:

<ProgressBar
        android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:max="100"
        android:backgroundTint="@color/white"
        android:layout_below="@+id/framelauout"
        android:indeterminateTint="#1a09d6"
        android:layout_marginTop="-7dp"/>

在您的活动中

progressBar = (ProgressBar) findViewById(R.id.progressbar);

显示/隐藏进度条是一样的

Showing/hiding the progress bar is the same

 progressBar.setVisibility(View.VISIBLE); // To show the ProgressBar 
 progressBar.setVisibility(View.INVISIBLE); // To hide the ProgressBar

这是它的外观示例图像:

Here is a sample image of what it would look like:

欲知更多详情:
1.参考一
2.参考二