Sun*_*l P 178 android progressdialog android-progressbar
我发现ProgressDialog
现在已经弃用了.除了之外,还有什么可以代替它ProgressBar
.我正在使用android studio版本2.3.3.
ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.show();
Run Code Online (Sandbox Code Playgroud)
Gow*_*n M 176
是的,在API level 26
它已被弃用,你可以使用progressBar
.
使用此代码以编程方式创建:
RelativeLayout layout = findViewById(R.id.display); //在此指定根布局ID
(要么)
RelativeLayout layout = findViewById(this);
RelativeLayout layout = findViewById(R.id.display); //specify here Root layout Id
Run Code Online (Sandbox Code Playgroud)
要禁用用户交互,只需添加以下代码即可
Run Code Online (Sandbox Code Playgroud)RelativeLayout layout = findViewById(this);
要恢复用户交互,您只需添加以下代码即可
android.R.attr.progressBarStyleSmall
仅供将来参考,android.R.attr.progressBarStyleHorizontal
改为API level 21
.
此下面的行代码仅适用于API 21 progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
解决方案2:另一种方式
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);
Run Code Online (Sandbox Code Playgroud)
Activity.java
progressBar.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)
1.参考一个
2.参考二
我希望这可以帮到你.
Ous*_*oua 27
此类在API级别26中已弃用.ProgressDialog是一个模式对话框,可防止用户与应用程序进行交互.您应该使用ProgressBar等进度指示器,而不是使用此类,可以将其嵌入到应用程序的UI中.或者,您可以使用通知来通知用户任务的进度.链接
Android O
由于Google
新的UI标准,它已被弃用
Kis*_*nga 26
您可以使用以下代码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);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Dev*_*e01 19
您可以简单地为进度条设计一个xml界面,并将其作为视图传递给AlertDialog,然后随时显示或关闭对话框.
progress.xml
<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>
Run Code Online (Sandbox Code Playgroud)
显示进度对话框的代码.只需复制此代码并将其粘贴到您的片段即可.
private void setDialog(boolean show){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//View view = getLayoutInflater().inflate(R.layout.progress);
builder.setView(R.layout.progress);
Dialog dialog = builder.create();
if (show)dialog.show();
else dialog.dismiss();
}
Run Code Online (Sandbox Code Playgroud)
然后只要想要显示progressdialog就调用该方法,并将true作为参数传递给它来显示它,或者将false传递给false以关闭对话框.
Sye*_*eem 15
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>
Run Code Online (Sandbox Code Playgroud)
下一步是创建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();
Run Code Online (Sandbox Code Playgroud)
现在剩下的就是在我们这样的点击事件中显示和隐藏这个对话框
dialog.show(); // to show this dialog
dialog.dismiss(); // to hide this dialog
Run Code Online (Sandbox Code Playgroud)
这就是它,它应该工作,因为你可以看到它很简单,很容易实现ProgressBar而不是ProgressDialog.现在您可以在Handler或ASyncTask中显示/关闭此对话框,它可以满足您的需要
Han*_*Han 12
是的,不推荐使用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
}
}
}
Run Code Online (Sandbox Code Playgroud)
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"
android:layout_above="@+id/no_jobs_pickups"/>
<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>
Run Code Online (Sandbox Code Playgroud)
在你的代码中:只是做var dialog = ProgressDialog.progressDialog(context)
显示: dialog.show()
隐藏: dialog.dismiss()
Ram*_*aya 11
好吧,如果你真的想违背他们的意愿,你仍然可以使用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>
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*ano 10
You don't need to import any custom library.
I prefer to use the modern AlertDialog
so this is the Kotlin version for the great answer posted by Kishan Donga in this page.
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
}
Run Code Online (Sandbox Code Playgroud)
val dialog = setProgressDialog(this, "Loading..")
dialog.show()
Run Code Online (Sandbox Code Playgroud)
ProgressBar是ProgressDialog的最佳替代方案.用于指示操作进度的用户界面元素.
有关详细信息,请参阅此Google文档: https ://developer.android.com/reference/android/widget/ProgressBar.html
归档时间: |
|
查看次数: |
147233 次 |
最近记录: |