alt text http://img252.imageshack.us/img252/643/snakesonadialog.png
嗨伙计,
所以我正在对应用程序进行最后的修改,整个应用程序仍有一个突出的问题.
我以ProgressDialogs的形式显示加载指示器,同时从Web中提取数据.由于某种原因,我的ProgressDialogs似乎嵌套在另一个空白对话框中.结果很俗气.
我的布局代码如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Theme.MyTheme.EpisodeList">
<ImageView android:src="@drawable/featured"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:layout_gravity="top"
android:shadowRadius="0"
android:shadowColor="#FFFFFF"
android:id="@+id/header_image"
/>
<ListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
我用来显示ProgressDialog的代码如下:
// In my class definition
private ProgressDialog p;
// in my onCreate method
p = new ProgressDialog(EpisodeActivity.this, ProgressDialog.STYLE_SPINNER);
p.setMessage("Loading...");
p.show();
从它的外观来看,似乎没什么好看的.我还附上一张照片来说明问题.
任何帮助表示赞赏!
解决方法:
经过几天的充实,我找到了处理ProgressDialogs的最佳方法.处理简单的ProgressDialogs在Android平台上肯定过于复杂,但这并不意味着我们有任何借口不与它们合作.希望未来会更好(连同文档).这是我的最佳实践.
在空白对话框内嵌入ProgressDialog的问题似乎是在2.1或2.2系统上运行的2.0之前的代码问题.我只能猜测show()方法也调用了super.show(),它在2.1和2.2上表现不同.
所以,我发现如果你不直接在ProgressDialog对象上调用show(),它最有效.相反,使用内置的showDialog()和onCreateDialog()方法来处理对话框.这是一段代码片段:
private static final int ID_DIALOG_LOADING = 0;
@Override
protected void onCreate(Bundle tedBundy) {
// Do stuff
showDialog(ID_DIALOG_LOADING);
// Do more stuff in a thread
}
@Override
public void run() {
// Do some stuff in this thread
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// Let adapters know the data under them have changed
try {
dimissDialog(ArticlesList.ID_DIALOG_LOADING);
removeDialog(ArticlesList.ID_DIALOG_LOADING);
} catch (Exception e) {}
}
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == ID_DIALOG_LOADING) {
ProgressDialog loadingDialog = new ProgressDialog(this);
loadingDialog.setMessage("Loading...");
loadingDialog.setIndeterminate(true);
loadingDialog.setCancelable(true);
return loadingDialog;
}
return super.onCreateDialog(id);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
try {
Log.i("ArticlesList.onSaveInstanceState", "Chirp chirp");
dismissDialog(ArticlesList.ID_DIALOG_LOADING);
removeDialog(ArticlesList.ID_DIALOG_LOADING);
} catch (Exception e) {
}
super.onSaveInstanceState(outState);
}
@Override
protected void onDestroy() {
try {
dismissDialog(ArticlesList.ID_DIALOG_LOADING);
removeDialog(ArticlesList.ID_DIALOG_LOADING);
} catch (Exception e) {
}
super.onDestroy();
}
@Override
protected void onPause() {
try {
dismissDialog(ArticlesList.ID_DIALOG_LOADING);
removeDialog(ArticlesList.ID_DIALOG_LOADING);
} catch (Exception e) {
}
super.onPause();
}
@Override
public void onDetachedFromWindow() {
try {
dismissDialog(ArticlesList.ID_DIALOG_LOADING);
removeDialog(ArticlesList.ID_DIALOG_LOADING);
} catch (Exception e) {
}
super.onDetachedFromWindow();
}
这是一个很长的代码段.您还将看到一堆重复的代码,它们不断尝试杀死该对话框.这样做是因为Android会在手机旋转时销毁并重新创建视图.希望在将来的版本中,只要用户旋转屏幕,窗口就不会被完成销毁和重新加载,而是实现onScreenRotate()方法.
在尝试了几种不同的方法之后,这个hacky解决方案似乎是唯一能够提供一致,快速删除ProgressDialog结果的解决方案.希望这能节省一些人未来的时间.