其实android自定义的dialog都已经足够好了,但是有时候产品还是会设计出类似于iPhone的圆角dialog,今天抽空就将自己写的总结出来,以后用的时候直接来扒代码就好了。
相信很多人也写过类似的,但是有时候出现一个默认dialog的背景,然后就看着好丑。就先扯到这儿,上我自己自定义运行的截图:
custom_dialog.jpg其实代码没有多少先写自定义的布局文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="136dp"
android:background="@drawable/white_shape_8"
android:orientation="vertical">
<LinearLayout
android:id="@+id/callback_dialog_ll_top"
android:layout_width="match_parent"
android:layout_height="36dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/callback_dialog_img_state"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="13dp"
android:layout_marginTop="16dp"
android:src="@mipmap/ic_success"/>
<TextView
android:id="@+id/callback_dialog_tv_title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginLeft="7dp"
android:layout_marginTop="16dp"
android:gravity="center_vertical"
android:text="成功"
android:textColor="#393939"
android:textSize="14sp"
android:textStyle="bold"/>
</LinearLayout>
<TextView
android:id="@+id/callback_dialog_tv_msg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="#393939"
android:textSize="14sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:background="#EDEDEF"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/callback_dialog_tv_negate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="12dp"
android:text="取消"
android:textColor="#9B9DB1"
android:textSize="15sp"/>
<TextView
android:id="@+id/callback_dialog_tv_dividers"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#EDEDEF"/>
<TextView
android:id="@+id/callback_dialog_tv_positive"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="12dp"
android:text="确定"
android:textColor="#007AFF"
android:textSize="15sp"/>
</LinearLayout>
</LinearLayout>
布局文件中引用的drawable文件white_shape_8:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFFF"/>
<!--圆角弧度可自己修改-->
<corners android:radius="8dp"/>
<stroke
android:width="0.5dp"
android:color="#EDEDEF"/>
</shape>
然后是自定义的custom,其中包含了是否显示标题、内容是什么、底部按钮显示1个还是2个,都可以在其中进行设置
/**
* 自定义Dialog
*/
public class CustomDialog extends Dialog {
private String title;
private String message;
private View.OnClickListener onNegateClickListener;
private View.OnClickListener onPositiveClickListener;
private String state;//0、警告;1、成功;2、失败;
private int topShow;//0、隐藏1、显示
public CustomDialog(Context context) {
super(context);
}
/**
* @param context 上下文
* @param theme 给dialog设置的主题
*/
public CustomDialog(Context context, int theme) {
super(context, theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.custom_dialog);
//设置dialog的大小
WindowManager m = getWindow().getWindowManager();
Display d = m.getDefaultDisplay();
WindowManager.LayoutParams p = getWindow().getAttributes();
p.width = d.getWidth() - 100; //设置dialog的宽度为当前手机屏幕的宽度-100
getWindow().setAttributes(p);
LinearLayout llTop = (LinearLayout) findViewById(R.id.callback_dialog_ll_top);
llTop.setVisibility(topShow);
ImageView stateImg = (ImageView) findViewById(R.id.callback_dialog_img_state);
if (!TextUtils.isEmpty(state)) {
stateImg.setVisibility(View.VISIBLE);
stateImg.setImageResource(state.equals("0") ? R.mipmap.ic_photo_smile : state.equals("1") ? R.mipmap.ic_success : R.mipmap.ic_fail);
} else {
stateImg.setVisibility(View.GONE);
}
TextView textTitle = (TextView) findViewById(R.id.callback_dialog_tv_title);
if (!TextUtils.isEmpty(title)) {
textTitle.setVisibility(View.VISIBLE);
textTitle.setText(title);
} else {
textTitle.setVisibility(View.GONE);
}
TextView textMsg = (TextView) findViewById(R.id.callback_dialog_tv_msg);
if (!TextUtils.isEmpty(message)) {
textMsg.setVisibility(View.VISIBLE);
textMsg.setText(message);
}
TextView divider = (TextView) findViewById(R.id.callback_dialog_tv_dividers);
TextView negate = (TextView) findViewById(R.id.callback_dialog_tv_negate);
if (onNegateClickListener != null) {
negate.setVisibility(View.VISIBLE);
negate.setOnClickListener(onNegateClickListener);
} else {
divider.setVisibility(View.GONE);
negate.setVisibility(View.GONE);
}
TextView positive = (TextView) findViewById(R.id.callback_dialog_tv_positive);
if (onPositiveClickListener != null) {
positive.setVisibility(View.VISIBLE);
positive.setOnClickListener(onPositiveClickListener);
} else {
divider.setVisibility(View.GONE);
positive.setVisibility(View.GONE);
}
}
public void setTitle(String title) {
this.title = title;
}
public void setMsg(String message) {
this.message = message;
}
public void setState(String state) {
this.state = state;
}
public void setTopShow(int topShow) {
this.topShow = topShow;
}
/**
* 确定按钮
*/
public void setOnPositiveListener(View.OnClickListener onPositiveClickListener) {
this.onPositiveClickListener = onPositiveClickListener;
}
/**
* 取消按钮
*/
public void setOnNegateListener(View.OnClickListener onNegateClickListener) {
this.onNegateClickListener = onNegateClickListener;
}
}
然后就可以在代码中引用了:
private void showCustomDialog() {
final CustomDialog customDialog = new CustomDialog(context, R.style.CustomDialog);//设置自定义背景
customDialog.setState("0");
customDialog.setTitle("组队提醒");
customDialog.setMsg("女神请求与您组队去打Boss!");
customDialog.setOnNegateListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "您拒绝了与女神组队去闯关!!!", Toast.LENGTH_SHORT).show();
customDialog.dismiss();
}
});
customDialog.setOnPositiveListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(context, "您已同意与女神组队去闯关!!!", Toast.LENGTH_SHORT).show();
customDialog.dismiss();
}
});
customDialog.show();
}
上面最主要的是引用的主题,需要在styles.xml中设置一个自定义的style:
<!--自定义用于去除背景-->
<style name="CustomDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">false</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>
</style>
OK,大功告成,如果觉得自定义的不够用,还可以自己修改、增加,目测一个项目有这一个也就够了~~~
网友评论
```
代码
```