ProgressDialog 已弃用.要使用的替代方法是什么? [英] ProgressDialog is deprecated.What is the alternate one to use?

查看:45
本文介绍了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 级别 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.参考二

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆