在开发过程中,有时需要自定义的Dialog,设计的需求是,按照屏幕的比例来显示Dialog,需要有圆角。效果如下图。
自定义代码Dialog 代码如下:
CustomDialog.class
public class CustomDialog extends Dialog {
private static int default_width = 300;//默认宽 dp
private static int default_height = 280;//默认高 dp
public CustomDialog(Context context, View layout, int style) {
this(context, default_width,default_height, layout, style);
}
/***
width--
*/
public CustomDialog(Context context, int width, height, View layout, int themeResId) {
super(context, themeResId);
setContentView(layout);
int width=DisplayUtils.dp2px(width);
int height=DisplayUtils.dp2px(height);
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.CENTER;
window.setAttributes(params);
DisplayMetrics dm = context.getResources().getDisplayMetrics();
int displayWidth = dm.widthPixels;
int displayHeight = dm.heightPixels;
android.view.WindowManager.LayoutParams p = getWindow().getAttributes(); //获取对话框当前的参数值
int
p.width = (int) (width/displayWidth); // 占屏幕宽的比例
p.height = (int) (height/displayHeight ); // 占屏幕高的比例
window.setAttributes(p); //设置生效
}
}
DisplayUtils
public class DisplayUtils {
/**
* 将px值转换为dp值
*/
public static int px2dp(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 将dp值转换为px值
*/
public static int dp2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 将px值转换为sp值
*/
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 将sp值转换为px值
*/
public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
/**
* 获取屏幕宽度
*/
public static int getScreenWidthPixels(Activity context) {
DisplayMetrics metric = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metric);
return metric.widthPixels;
}
/**
* 获取屏幕高度
*/
public static int getScreenHeightPixels(Activity context) {
DisplayMetrics metric = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metric);
return metric.heightPixels;
}
}
然后就是显示的问题了
UIHelper.class
public class UIHelper {
private static LinearLayout layout_del;
private static Button btn;
private static Dialog dialog;
public static void showKonwDialog(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.dialog_konw, null);
final CustomDialog builder = new CustomDialog(context, 0, 0, view, R.style.dialog);
//设置对话框显示的View
builder.show();
layout_del = (LinearLayout) builder.findViewById(R.id.dialog_layout_del);
btn = (Button) builder.findViewById(R.id.dialog_konw_btn);
layout_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (builder.isShowing()) {
builder.dismiss();
}
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
builder.dismiss();
}
});
}
}
Style
在values文件夹下的style 中添加样式。。
<!--***************Dialog***************-->
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.6</item>
</style>
dialog_konw.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/common_bg_trans"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:background="@drawable/shape_dialog_bg"
android:orientation="vertical">
<LinearLayout
android:id="@+id/dialog_layout_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingBottom="@dimen/y20"
android:paddingLeft="@dimen/x22"
android:paddingRight="@dimen/x22"
android:paddingTop="@dimen/y20">
<ImageView
android:layout_width="@dimen/y22"
android:layout_height="@dimen/y22"
app:srcCompat="@drawable/ic_pop_icon_del" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_marginLeft="@dimen/x76"
android:layout_marginRight="@dimen/x76"
android:layout_height="@dimen/y84"
android:singleLine="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/y20"
android:background="@color/com_bg_gray_eeeeee"
android:hint="@string/input_your_name"
android:paddingLeft="@dimen/x29"
android:textColorHint="@color/com_tv_gray_666666"
android:textSize="@dimen/comm_tv_14" />
<EditText
android:layout_width="match_parent"
android:layout_marginLeft="@dimen/x76"
android:layout_marginRight="@dimen/x76"
android:layout_height="@dimen/y84"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/y20"
android:singleLine="true"
android:background="@color/com_bg_gray_eeeeee"
android:hint="@string/input_your_phone"
android:paddingLeft="@dimen/x29"
android:textColorHint="@color/com_tv_gray_666666"
android:textSize="@dimen/comm_tv_14" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sex"
android:textColor="@color/com_tv_black_333333"
android:textSize="@dimen/comm_tv_14" />
<com.aoben.qproj.widget.CustomRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/x71" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/plz_wait"
android:textColor="@color/com_tv_gray_666666"
android:textSize="@dimen/comm_tv_10" />
<Button
android:id="@+id/dialog_konw_btn"
android:layout_width="match_parent"
android:layout_marginLeft="@dimen/x76"
android:layout_marginRight="@dimen/x76"
android:layout_height="@dimen/y80"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="@dimen/y70"
android:layout_marginTop="@dimen/y38"
android:background="@drawable/selector_btn_bg"
android:text="@string/insure"
android:textColor="@color/com_tv_white_ffffff"
android:textSize="@dimen/comm_tv_15" />
</LinearLayout>
</RelativeLayout>
x22 y26 这些是上一篇文章,关于屏幕适配有说到。
shape_dialog_bg.xml
实现圆角,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="@dimen/comm_dim_4" />
<solid android:color="@color/com_bg_white_ffffff"/>
</shape>
这样就实现了,文章开头的效果图中的自定义dialog.