本文介绍了不推荐使用ProgressDialog.可以使用什么替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现ProgressDialog现在已被弃用.除了ProgressBar以外,还有什么可以替代.我正在使用Android Studio版本2.3.3.

I have come across to see that ProgressDialog is now deprecated. What would be alternate one to use in place of that apart from ProgressBar.I am using android studio version 2.3.3.

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

推荐答案

是的,在API level 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.progressBarStyleHorizontal.

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.参考二

For more details:
1. Reference one
2. Reference Two

这篇关于不推荐使用ProgressDialog.可以使用什么替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:41