我发现ProgressDialog现在已经被弃用了。除了ProgressBar,还有什么替代工具可以代替它。 我使用的是android studio 2.3.3版。
ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.show();
我发现ProgressDialog现在已经被弃用了。除了ProgressBar,还有什么替代工具可以代替它。 我使用的是android studio 2.3.3版。
ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.show();
当前回答
你可以使用AlertDialog作为ProgressDialog,参考下面的ProgressDialog代码。无论何时显示进度对话框,都需要调用此函数。
代码:
public void setProgressDialog() {
int llPadding = 30;
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setPadding(llPadding, llPadding, llPadding, llPadding);
ll.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
llParam.gravity = Gravity.CENTER;
ll.setLayoutParams(llParam);
ProgressBar progressBar = new ProgressBar(this);
progressBar.setIndeterminate(true);
progressBar.setPadding(0, 0, llPadding, 0);
progressBar.setLayoutParams(llParam);
llParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
llParam.gravity = Gravity.CENTER;
TextView tvText = new TextView(this);
tvText.setText("Loading ...");
tvText.setTextColor(Color.parseColor("#000000"));
tvText.setTextSize(20);
tvText.setLayoutParams(llParam);
ll.addView(progressBar);
ll.addView(tvText);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setView(ll);
AlertDialog dialog = builder.create();
dialog.show();
Window window = dialog.getWindow();
if (window != null) {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(dialog.getWindow().getAttributes());
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(layoutParams);
}
}
输出:
其他回答
ProgressBar非常简单易用, 我打算制作一个同样简单的进度对话框。 第一步是你可以为你想要显示的对话框创建XML布局,假设我们命名这个布局
layout_loading_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp">
<ProgressBar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="center"
android:text="Please wait! This may take a moment." />
</LinearLayout>
下一步是创建AlertDialog,它将用ProgressBar显示这个布局
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false); // if you want user to wait for some process to finish,
builder.setView(R.layout.layout_loading_dialog);
AlertDialog dialog = builder.create();
现在剩下的就是在点击事件中显示和隐藏这个对话框 像这样
dialog.show(); // to show this dialog
dialog.dismiss(); // to hide this dialog
这就是它,它应该工作,因为你可以看到它是相当简单和容易实现的ProgressBar而不是ProgressDialog。 现在你可以在Handler或ASyncTask中显示/取消这个对话框,这取决于你的需要
ProgressBar是ProgressDialog的最佳选择。 指示操作进度的用户界面元素。
欲了解更多信息,请参阅谷歌文档:https://developer.android.com/reference/android/widget/ProgressBar.html
正如文档页中提到的,替代方案是ProgressBar。ProgressDialog的外观可以通过在AlertDialog中放置ProgressBar来复制。
你仍然可以使用它,但Android不希望你使用它,这就是它被弃用的原因。所以你应该考虑用另一种方式解决你的问题,比如在你的布局中嵌入一个ProgressBar。
在进度对话框中,用户不能做任何工作。所有后台进程在进度对话框中停止。因此,建议使用进度条代替进度对话框。
下面是我的不确定进度对话框版本:
layout_loading_dialog.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:padding="20dp">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="center"
android:textAlignment="textStart"
android:id="@+id/message"
tools:text="Please wait..." />
</LinearLayout>
IndeterminateProgressDialog.kt:
class IndeterminateProgressDialog(context: Context) : AlertDialog(context) {
private val messageTextView: TextView
init {
val view = LayoutInflater.from(context).inflate(R.layout.layout_loading_dialog, null)
messageTextView = view.findViewById(R.id.message)
setView(view)
}
override fun setMessage(message: CharSequence?) {
this.messageTextView.text = message.toString()
}
}
用法:
val dialog = IndeterminateProgressDialog(context)
dialog.setMessage("Please wait...")
dialog.setCanceledOnTouchOutside(false)
dialog.setCancelable(false)
dialog.show()