直接在代码层实现自定义dialog,只要几行代码就够了,做个记录
private void showDialog() {
if (mDialog == null) {
mDialog = new AlertDialog.Builder(this, R.style.MyDialog).create();
}
mDialog.show();
View dialogView = LayoutInflater.from(this).inflate(R.layout.item_complete_dialog, null);
ViewGroup parent = (ViewGroup) dialogView.getParent();
if (parent != null) {
parent.removeAllViews();
}
mDialog.setCanceledOnTouchOutside(false);
mDialog.getWindow().setContentView(dialogView);
mDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
dialogView.findViewById(R.id.id_iv_close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
}
});
dialogView.findViewById(R.id.yes).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainHomeActivity.this, UserInfoActivity.class));
mDialog.dismiss();
}
});
}
item_complete_dialog 布局 有点乱。。。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="280dp"
android:layout_height="170dp"
android:layout_centerInParent="true"
android:background="@drawable/bg_login_code"
android:orientation="vertical">
<ImageView
android:id="@+id/id_iv_close"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="right"
android:layout_margin="8dp"
android:scaleType="center"
android:src="@mipmap/ic_login_close" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="请先完善个人信息,审核更快捷,所在城市请精确到区县。"
android:textColor="#333333"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="15dp"
android:background="#E4E4E4"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="@+id/yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:gravity="center"
android:singleLine="true"
android:text="立即完善"
android:textColor="#FC4E3B"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
MyDialog 的样式
<style name="MyDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>