我发现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();
当前回答
Kotlin和androidx DialogFragment的另一个解决方案
通过使用DialogFragment,你不必每次都在需要显示对话框的每个屏幕上调整窗口的大小,我认为这是一个更干净的解决方案
创建对话框类
class BaseProgressDialog : DialogFragment() {
companion object {
const val DIALOG_TAG = "BaseProgressDialogFragment"
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = AlertDialog.Builder(it, R.style.MainAlertDialog)
val inflater = requireActivity().layoutInflater
val dialogView = inflater.inflate(R.layout.progress_dialog, null)
builder.setView(dialogView)
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
dialog?.setCanceledOnTouchOutside(false)
dialog?.setCancelable(false)
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onResume() {
super.onResume()
val size = resources.getDimensionPixelSize(R.dimen.size_80dp)
dialog?.window?.setLayout(size, size)
}
}
在styles.xml中创建样式
<style name="MainAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">@drawable/bg_alert_dialog</item>
</style>
为对话框bg_alert_dialog.xml创建背景
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/colorSecondary"/>
<corners android:radius="@dimen/radius_10dp" />
</shape>
为对话框progress_dialog.xml创建布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_alert_dialog"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/padding_16dp">
<com.google.android.material.progressindicator.CircularProgressIndicator
android:layout_width="@dimen/size_40dp"
android:layout_height="@dimen/size_40dp"
android:indeterminate="true"
app:indicatorColor="?attr/colorPrimary" />
</LinearLayout>
调用片段中的对话框
private var progressDialog = BaseProgressDialog()
activity?.let {
progressDialog.show(it.supportFragmentManager, BaseProgressDialog.DIALOG_TAG)
}
这是它看起来的样子:
其他回答
ProgressBar是ProgressDialog的最佳选择。 指示操作进度的用户界面元素。
欲了解更多信息,请参阅谷歌文档:https://developer.android.com/reference/android/widget/ProgressBar.html
Kotlin和androidx DialogFragment的另一个解决方案
通过使用DialogFragment,你不必每次都在需要显示对话框的每个屏幕上调整窗口的大小,我认为这是一个更干净的解决方案
创建对话框类
class BaseProgressDialog : DialogFragment() {
companion object {
const val DIALOG_TAG = "BaseProgressDialogFragment"
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = AlertDialog.Builder(it, R.style.MainAlertDialog)
val inflater = requireActivity().layoutInflater
val dialogView = inflater.inflate(R.layout.progress_dialog, null)
builder.setView(dialogView)
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
dialog?.setCanceledOnTouchOutside(false)
dialog?.setCancelable(false)
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onResume() {
super.onResume()
val size = resources.getDimensionPixelSize(R.dimen.size_80dp)
dialog?.window?.setLayout(size, size)
}
}
在styles.xml中创建样式
<style name="MainAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">@drawable/bg_alert_dialog</item>
</style>
为对话框bg_alert_dialog.xml创建背景
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/colorSecondary"/>
<corners android:radius="@dimen/radius_10dp" />
</shape>
为对话框progress_dialog.xml创建布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_alert_dialog"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/padding_16dp">
<com.google.android.material.progressindicator.CircularProgressIndicator
android:layout_width="@dimen/size_40dp"
android:layout_height="@dimen/size_40dp"
android:indeterminate="true"
app:indicatorColor="?attr/colorPrimary" />
</LinearLayout>
调用片段中的对话框
private var progressDialog = BaseProgressDialog()
activity?.let {
progressDialog.show(it.supportFragmentManager, BaseProgressDialog.DIALOG_TAG)
}
这是它看起来的样子:
它可能对其他人有帮助。
许多流行的应用程序都有不同的方法来显示任何事情的进展,如网络请求,文件加载等。加载旋转器不会显示已加载或剩余的内容有多少。从UI/UX的角度来看,有一段时间的不确定性是不好的。许多流行的应用程序(Facebook, Linkedin等)通过首先显示基本的UI显示来解决这个问题。然后加载的内容逐渐填充在屏幕上。
我已经在我的应用程序中使用了shimmer来解决这个问题。
有一篇关于这方面的好文章,它将对其他人有益
您不需要导入任何自定义库。
我更喜欢使用现代的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()
输出:
使用这个简单的技巧
/ /初始化
val dialog : Dialog = Dialog(this)
/ /设置布局
dialog.setContentView(R.layout.view_loading)
/ /显示对话框
dialog.show()
//删除白色背景
dialog.window.setbackgroundDrawable(ColorDrawable(0))