定义style
<style name="LoadProgress" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:background">#00000000</item>
<item name="android:windowBackground">@android:color/transparent</item>
<!--<item name="android:windowAnimationStyle">@style/ActionSheetAnimation</item> -->
</style>
创建Dialog
Dialog protocolDialog = new Dialog(context, R.style.LoadProgress);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) inflater.inflate(
R.layout.dialog_loadprogress, null);
protocolDialog.setCanceledOnTouchOutside(false);
protocolDialog.setContentView(layout);
protocolDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
} else {
return true; //默认返回 false,这里false不能屏蔽返回键,改成true就可以了
}
}
});
布局文件dialog_loadprogress,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<ProgressBar
android:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="false"
/>
</LinearLayout>
调用
public void show() {
if (protocolDialog != null && !protocolDialog.isShowing() && context != null && !context.isFinishing()) {
protocolDialog.show();
}
}
public void close() {
if (protocolDialog != null) {
if (protocolDialog.isShowing()) {
protocolDialog.dismiss();
}
}
网友评论