自定义dialog去除黑色背景
2017-07-09 本文已影响0人
我是你森哥哥
QQ截图20170709134403.png
1. 自定义MyDialog 继承Dialog
在构造方法中引用自定义属性( super(context, R.style.MyDialog);)
public class public class MyDialog extends Dialog {
private String title;
private OnOkListener onOkListener;
public MyDialog(Context context, OnOkListener onOkListener) {
super(context, R.style.MyDialog);
this.onOkListener = onOkListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_dialog_exit);
TextView tv_itemdialog_ok = (TextView) findViewById((R.id.tv_itemdialog_ok));
TextView tv_itemdialog_close = (TextView) findViewById((R.id.tv_itemdialog_close));
tv_itemdialog_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
tv_itemdialog_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
onOkListener.okListener();
}
});
}
}
2. 自定义属性 去除黑色背景
<style name="MyDialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
3. dialog布局文件
<?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="wrap_content"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="@dimen/x280"
android:layout_height="wrap_content"
android:background="@color/green3"
android:orientation="vertical">
<TextView
android:id="@+id/tv_itemdialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/y15"
android:text="当前正在面聊,是否确定退出?"
android:textColor="@color/green1"
android:textSize="@dimen/x16" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/y5">
<TextView
android:id="@+id/tv_itemdialog_ok"
android:layout_width="@dimen/x100"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/y20"
android:background="@drawable/selector_btn_square"
android:gravity="center"
android:padding="@dimen/y1"
android:text="确定"
android:textColor="@color/bg_White"
android:textSize="@dimen/x16" />
<TextView
android:id="@+id/tv_itemdialog_close"
android:layout_width="@dimen/x100"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/y20"
android:background="@drawable/selector_btn_square"
android:gravity="center"
android:padding="@dimen/y1"
android:text="取消"
android:textColor="@color/bg_White"
android:textSize="@dimen/x16" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/y10"/>
</LinearLayout>
</LinearLayout>