注:本文转自 http://blog.163.com/shaocpa@126/blog/static/55357757201241102525166/
<style name="Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item> <item name="android:windowBackground">@android:drawable/panel_background</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> </style>
<style name="Theme_dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowNoTitle">true</item> </style>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/pp_bg_dialog" android:gravity="center"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/progressbar_normal"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/text_white_small" android:layout_marginTop="5dp" android:text="正在删除..." android:id="@+id/message"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10dp"/> <solid android:color="#55000000"/> </shape>
public class CustomDialog extends Dialog { 2 private static int default_width = 160; //默认宽度 private static int default_height = 120;//默认高度 public CustomDialog(Context context, int layout, int style) { this(context, default_width, default_height, layout, style); } public CustomDialog(Context context, int width, int height, int layout, int style) { super(context, style);12 //set content setContentView(layout); //set window params Window window = getWindow(); WindowManager.LayoutParams params = window.getAttributes(); //set width,height by density and gravity float density = getDensity(context); params.width = (int) (width*density); params.height = (int) (height*density); params.gravity = Gravity.CENTER; window.setAttributes(params); } private float getDensity(Context context) { Resources resources = context.getResources(); DisplayMetrics dm = resources.getDisplayMetrics(); return dm.density; } }
public class CustomDialogDemoActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test); CustomDialog dialog1 = new CustomDialog(this, R.layout.common_dialog, R.style.Theme_dialog);//Dialog使用默认大小(160) CustomDialog dialog2 = new CustomDialog(this, 180, 180, R.layout.common_dialog, R.style.Theme_dialog); dialog2.show();//显示Dialog //如果要修改Dialog中的某个View,比如把"正在删除..."改为"加载中..." TextView mMessage = (TextView) dialog2.findViewById(R.id.message); mMessage.setText("加载中..."); } }