dialog的应用
第一步:重写dialog:
①代码:
public static Dialog creatRequestDialog(final Context context, String tip) { final Dialog dialog = new Dialog(context, R.style.dialog); // dialog.setCancelable(false); // dialog.setCanceledOnTouchOutside(false); dialog.setContentView(R.layout.dialog_layout); Window window = dialog.getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); int width = Utils.getScreenWidth(context); lp.width = (int) (0.5 * width); TextView titleTxtv = (TextView) dialog.findViewById(R.id.tvLoad); titleTxtv.setText(tip); return dialog; }
②dialog的style设置:
<style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowIsFloating">true</item> <!-- 设置未浮动窗口 --> <item name="android:windowFrame">@null</item> <!-- 设置无边框 --> <item name="android:windowNoTitle">true</item> <!-- 设置无标题 --> <item name="android:windowBackground">@color/sc_transparent_background</item> <!-- 设置完全透明 --> <item name="android:backgroundDimEnabled">true</item> <!-- 设置屏幕变暗 --> </style>
③dialog的布局设置:
<?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:background="@drawable/loading_bg" android:gravity="center_horizontal" android:orientation="vertical" android:paddingBottom="15dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="15dp" > <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateDrawable="@drawable/frame_loading" /> <TextView android:id="@+id/tvLoad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="5dp" android:textColor="#ffffffff" android:textSize="17.0sp" /> </LinearLayout>
第二步:dialog的应用:
public void showRequestDialog(String msg) { if (mDialog != null) { mDialog.dismiss(); mDialog = null; } mDialog = DialogFactory.creatRequestDialog(this, msg); // mDialog.setCancelable(false); mDialog.setCanceledOnTouchOutside(false);//防止4.0系统点击dialog之外时出现崩溃问题 mDialog.show(); } //关闭Dialog public void closeRequestDialog() { if (mDialog != null) { mDialog.dismiss(); mDialog = null; } } //回退 @Override public void onBackPressed() { super.onBackPressed(); finish(); }