我发现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();
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);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
android.R.attr.progressBarStyleSmall
更改为android.R.attr.progressBarStyleHorizontal
。API级别21以上
。progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
<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);
progressBar.setVisibility(View.VISIBLE); // To show the ProgressBar
progressBar.setVisibility(View.INVISIBLE); // To hide the ProgressBar
您可以使用 AlertDialog
来替代 ProgressDialog
。请参考以下代码来使用 ProgressDialog
。每当您展示进度对话框时,都需要调用此函数。
代码:
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
public class ProgressHelper {
private static AlertDialog dialog = null;
public static void showDialog(Context context, String message) {
if(dialog == null){
int llPadding = 30;
LinearLayout ll = new LinearLayout(context);
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(context);
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(context);
tvText.setText(message);
tvText.setTextColor(Color.parseColor("#000000"));
tvText.setTextSize(20);
tvText.setLayoutParams(llParam);
ll.addView(progressBar);
ll.addView(tvText);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
builder.setView(ll);
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);
}
}
}
public static boolean isDialogVisible(){
if(dialog != null){
return dialog.isShowing();
}else {
return false;
}
}
public static void dismissDialog(){
if(dialog != null){
dialog.dismiss();
dialog = null;
}
}
}
输出:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="13dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/loader"
android:layout_marginEnd="5dp"
android:layout_width="45dp"
android:layout_height="45dp" />
<TextView
android:layout_width="wrap_content"
android:text="Loading..."
android:textAppearance="?android:textAppearanceSmall"
android:layout_gravity="center_vertical"
android:id="@+id/loading_msg"
android:layout_toEndOf="@+id/loader"
android:layout_height="wrap_content" />
</LinearLayout>
这是显示进度对话框的代码。只需将此代码复制并粘贴到您的片段中即可。
Dialog dialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(R.layout.progress);
// This should be called once in your Fragment's onViewCreated() or in Activity onCreate() method to avoid dialog duplicates.
dialog = builder.create();
}
// This method is used to control the progress dialog.
private void setDialog(boolean show){
if (show)dialog.show();
else dialog.dismiss();
}
每当您想显示进度对话框时,只需调用该方法,并将true作为参数传递以显示它,或将false传递以关闭对话框。
this
,这样它就会变成AlertDialog.Builder builder = new AlertDialog.Builder(this);
。请确保你复制的是正确的代码。 - devmike01layout_toEndOf
пЉМеЫ†дЄЇеЃГдЄНйАВзФ®дЇОLinearLayout
гАВ - Mark是的,ProgressDialog已经被弃用了,但Dialog并没有。
您可以将自己的XML文件(包含进度条和加载文本)填充到对话框对象中,然后使用show()
和dismiss()
函数显示或隐藏它。
以下是一个示例(Kotlin):
ProgressDialog类:
class ProgressDialog {
companion object {
fun progressDialog(context: Context): Dialog{
val dialog = Dialog(context)
val inflate = LayoutInflater.from(context).inflate(R.layout.progress_dialog, null)
dialog.setContentView(inflate)
dialog.setCancelable(false)
dialog.window!!.setBackgroundDrawable(
ColorDrawable(Color.TRANSPARENT))
return dialog
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:background="#fff"
android:padding="13dp"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="100dp"
android:layout_margin="7dp"
android:layout_height="100dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="7dp"
android:layout_toEndOf="@+id/progressBar"
android:text="Loading..." />
</RelativeLayout>
在你的代码中:
只需执行 var dialog = ProgressDialog.progressDialog(context)
显示进度对话框:dialog.show()
隐藏进度对话框:dialog.dismiss()
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中显示/关闭此对话框框,具体取决于您的需求。
progress_dialog.show();
更改为 builder.show();
。谢谢。 - Matheus Mirandashow()
/dismiss()
上,我没有放置builder.show()
,因为这样做我们将不得不执行dialog.dismiss()
,这对于喜欢保持代码简单易懂的人来说有点困惑。此外,builder.show()
返回AlertDialog
对象,我们将不得不保存该引用,如AlertDialog dialog = builder.show();
,然后执行dialog.dismiss()
,更容易的方法是让builder.create()
返回某个AlertDialog
对象,然后使用该对象来显示和隐藏对话框。 - Syed Naeem您不需要导入任何自定义库。
我喜欢使用现代的 AlertDialog
,所以这是Kishan Donga在此页面发布的绝佳答案的Kotlin版本。
fun setProgressDialog(context:Context, message:String):AlertDialog {
val llPadding = 30
val ll = LinearLayout(context)
ll.orientation = LinearLayout.HORIZONTAL
ll.setPadding(llPadding, llPadding, llPadding, llPadding)
ll.gravity = Gravity.CENTER
var llParam = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
llParam.gravity = Gravity.CENTER
ll.layoutParams = llParam
val progressBar = ProgressBar(context)
progressBar.isIndeterminate = true
progressBar.setPadding(0, 0, llPadding, 0)
progressBar.layoutParams = llParam
llParam = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
llParam.gravity = Gravity.CENTER
val tvText = TextView(context)
tvText.text = message
tvText.setTextColor(Color.parseColor("#000000"))
tvText.textSize = 20.toFloat()
tvText.layoutParams = llParam
ll.addView(progressBar)
ll.addView(tvText)
val builder = AlertDialog.Builder(context)
builder.setCancelable(true)
builder.setView(ll)
val dialog = builder.create()
val window = dialog.window
if (window != null) {
val layoutParams = WindowManager.LayoutParams()
layoutParams.copyFrom(dialog.window?.attributes)
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT
layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT
dialog.window?.attributes = layoutParams
}
return dialog
}
val dialog = setProgressDialog(this, "Loading..")
dialog.show()
view!!.context
。 - Alessandro Ornano如果你真的想违背他们的意愿,你仍然可以使用Sweet Alert Dialog或者自己创建一个。
progress_dialog_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="64dp" >
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:gravity="center|left"
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/black"
android:textSize="18sp"
android:text="Downloading data. Please wait.." />
</TableRow>
</RelativeLayout>
Java 代码:
AlertDialog b;
AlertDialog.Builder dialogBuilder;
public void ShowProgressDialog() {
dialogBuilder = new AlertDialog.Builder(DataDownloadActivity.this);
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View dialogView = inflater.inflate(R.layout.progress_dialog_layout, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setCancelable(false);
b = dialogBuilder.create();
b.show();
}
public void HideProgressDialog(){
b.dismiss();
}
HideProgress
没有任何作用。 - Saeed NeamatiTableRow
和使用水平 LinearLayout
是一样的,只要父级不是 TableLayout
。https://developer.android.com/reference/android/widget/TableRow - fmaccaroniProgressBar
。通过将一个ProgressBar
放入一个AlertDialog
中,可以复制ProgressDialog
的外观。ProgressDialog
,但Android不希望您使用它,这就是为什么它被弃用的原因。因此,您应该考虑另一种解决问题的方法,例如将ProgressBar
嵌入您的Layout
中。