效果图:修改系统默认的Dialog.
1,修改样式,在style.xml中添加一下代码
<style name="DialogTheme" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
2.自定义Dialog类.
public class MyDialog extends Dialog {
private static int default_width = 160; // 默认宽度
private static int default_height = 120;// 默认高度
public MyDialog(Context context) {
super(context);
}
public MyDialog(Context context,int layout,int style){
this(context, default_width, default_height, layout, style);
}
public MyDialog(Context context,int width,int height,int layout,int style) {
super(context, style);
setContentView(layout);
//设置窗口属性
Window window=getWindow();
WindowManager.LayoutParams lp=window.getAttributes();
//设置宽度,高度,密度,对其方式
float density=getDensity(context);
lp.width=(int)(width*density);
lp.height=(int) (height*density);
lp.gravity=Gravity.CENTER;
window.setAttributes(lp);
}
/**
* 获取显示密度
*
* @param context
* @return
*/
public float getDensity(Context context) {
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
return dm.density;
}
}
3.新建一个自定义布局 dialog_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:background="@drawable/dialog_bg"
android:gravity="center"
android:orientation="vertical" >
<ProgressBar
style="@style/DialogTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="正在执行"
/>
</LinearLayout>
4,布局文件中的资源文件
<?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>
5,在MainActivity中使用
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyDialog mD=new MyDialog(this, R.layout.dialog_layout, R.style.DialogTheme);
mD.show();
}
}
android自定义Dialog
原文:http://www.cnblogs.com/aibuli/p/f67122d84df60eb5becc70eddcb4873c.html