Android 如何在ProgressDialog中轻松居中显示进度指示器(当没有传递标题/文本时)
调用Android 如何在ProgressDialog中轻松居中显示进度指示器(当没有传递标题/文本时),android,progressdialog,Android,Progressdialog,调用progressDialog=progressDialog.show时(this,null,null,true)通常开发人员只希望显示进度指示图像,并且通常希望它位于窗口的中心(至少从常规UI设计的角度来看)。 但是图像太偏左了,似乎右侧的某些填充/边距仍在为右侧的(可选)文本计算,尽管我们没有将任何文本作为参数传递。 这只会让开发人员的生活变得更轻松:)因此我们不需要创建自定义对话框,而只是为了让进度指示器在默认情况下居中 (我将此作为一项功能请求提交到;如果您还希望看到此功能得到改进,请
progressDialog=progressDialog.show时(this,null,null,true)代码>通常开发人员只希望显示进度指示图像,并且通常希望它位于窗口的中心(至少从常规UI设计的角度来看)。
但是图像太偏左了,似乎右侧的某些填充/边距仍在为右侧的(可选)文本计算,尽管我们没有将任何文本作为参数传递。
这只会让开发人员的生活变得更轻松:)因此我们不需要创建自定义对话框,而只是为了让进度指示器在默认情况下居中
(我将此作为一项功能请求提交到;如果您还希望看到此功能得到改进,请将其添加为星号)
现在我的问题是:
如何轻松地将进度图像居中,而不必完全创建自己的自定义警报对话框类?我可能忽略了哪些参数
此外,如何将背景设置为透明
我还想知道:
我自己还没有尝试过,但是如果你不能创建自定义动画,这意味着如果你想要一种动画进度指示器,你总是需要扩展ProgressDialog类?
但是看看ProgressDialog类,除了常规的drawables()之外,我没有发现任何东西,它们没有在那里使用AnimatedDrawable。我做了一些测试,我觉得实现这一点的最好方法是做一个自定义的对话框
这是我所做的一个例子。这将回答问题2,但会让您了解如何解决问题1
public class MyProgressDialog extends Dialog {
public static MyProgressDialog show(Context context, CharSequence title,
CharSequence message) {
return show(context, title, message, false);
}
public static MyProgressDialog show(Context context, CharSequence title,
CharSequence message, boolean indeterminate) {
return show(context, title, message, indeterminate, false, null);
}
public static MyProgressDialog show(Context context, CharSequence title,
CharSequence message, boolean indeterminate, boolean cancelable) {
return show(context, title, message, indeterminate, cancelable, null);
}
public static MyProgressDialog show(Context context, CharSequence title,
CharSequence message, boolean indeterminate,
boolean cancelable, OnCancelListener cancelListener) {
MyProgressDialog dialog = new MyProgressDialog(context);
dialog.setTitle(title);
dialog.setCancelable(cancelable);
dialog.setOnCancelListener(cancelListener);
/* The next line will add the ProgressBar to the dialog. */
dialog.addContentView(new ProgressBar(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
dialog.show();
return dialog;
}
public MyProgressDialog(Context context) {
super(context, R.style.NewDialog);
}
}
所有的静态方法都来自link,没有什么奇怪的,但神奇的是构造函数。
检查是否将样式作为参数传递。这种风格如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="NewDialog" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:background">@android:color/transparent</item>
</style>
</resources>
@空的
@android:彩色/透明
真的
@空的
@空的
@android:style/Animation.Dialog
状态未指定|调整盘
假的
@android:彩色/透明
其结果是ProgressBar
在屏幕中央旋转。没有backgroundDim
和对话框。简单且可自定义的方式:
//create Dialog by using below method
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG1_KEY: {
Dialog dialog = new Dialog(this,R.style.NewDialog);
dialog.setContentView(R.layout.progress);
dialog.setCancelable(true);
return dialog;
}
}
return null;
}
//then in your onCreate () you can call like below
@Override
public void onCreate(Bundle savedInstatncestate)
{
final Handler mHandler = new Handler();
showDialog(DIALOG1_KEY);
new Thread() {
public void run() {
try {
sleep(3000);
Runnable r = new Runnable()
{
public void run()
{
//do your background process
dismissDialog(DIALOG1_KEY);
}
};
mHandler.post(r);
} catch (Exception e) {
}
}
}.start();
}
定义动画:
(res/drawable/loader_anim.xml)
更多信息:在我的案例中,使用ProgressBar并将其添加到LinearLayout的工作如下:
ProgressBar mSpinner = new ProgressBar(this);
mSpinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mSpinner.setBackgroundResource(R.drawable.loading_1);
mSpinner.setIndeterminate(true);
< P>我使用下面的方法,它不需要布局文件,并且在屏幕中间放置一个中心的、无边界的阻挡进度条。
private ProgressDialog progressDialog;
setUIToWait(true);
...long process...
setUIToWait(false);
private void setUIToWait(boolean wait) {
if (wait) {
progressDialog=ProgressDialog.show(this,null,null);
progressDialog.setContentView(new ProgressBar(this));
} else {
progressDialog.dismiss();
}
}
如果只想显示不确定的进度条
ProgressDialog progressDialog = ProgressDialog.show(this, null, null, true, false);
progressDialog.setContentView(R.layout.progress_layout);
并创建一个名为“progress_layout.xml”的布局xml文件
只需执行以下方法即可完成
在res->values->style中添加以下代码
<style name="MyGravity" parent="android:Theme.Material.Dialog" ></style>
您可以始终在所有活动中添加一个进度条,以便显示以进度为中心的对话框。在acitivity.xml中使用以下内容:
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_centerInParent="true"
android:visibility="gone"/>
现在,当您想要显示ProgressBar时,只需将其可见性设置为visible,若要取消,请将visibility设置为gone
bar.setVisibility(View.VISIBLE);
bar.setVisibility(View.GONE);
@麦卡斯没有。。。我还尝试了:progressDialog=newmyprogressdialog(ItemListActivity.this);progressDialog.show(ItemListActivity.this,“title”,“message”);progressDialog.disclose()代码>它显示正确,但不会关闭…@jul我也很难关闭自定义对话框,你们中的任何一位可以解释一下如何静态地调用它吗?谢谢,请在声明样式时添加父项。此样式可能会在新API中产生很多问题:parent=“@android:style/Theme.Dialog”@Millec8-要关闭该对话框,请执行以下操作-MyProgressDialog;progressDialog=MyProgressDialog.show(ItemListActivity.this,“标题”、“消息”);progressDialog.disclose();对于任何想知道为什么它没有完全集中在屏幕上的人来说,这是因为标题栏仍然在显示-删除它:true
正是我想要的!感谢这仍然会根据主题添加白色背景。我认为这是最简单的方法。做得很好,你帮我节省了很多时间!非常简短,最简单的方法。仅spinner,这是我一直在寻找的,很好的答案:)tnx,对我来说是最好的答案。此解决方案适用于>21 API
ProgressDialog progressDialog = ProgressDialog.show(this, null, null, true, false);
progressDialog.setContentView(R.layout.progress_layout);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
<style name="MyGravity" parent="android:Theme.Material.Dialog" ></style>
ProgressDialog dialog = new ProgressDialog(ctx, R.style.MyGravity);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_centerInParent="true"
android:visibility="gone"/>
ProgressBar bar = new Progress();
bar = (ProgressBar) findViewById(R.id.progressBar);
bar.setVisibility(View.VISIBLE);
bar.setVisibility(View.GONE);