第一步:首先写出自定义dialog的style文件。
//退出自定义dialog的style
<style name="style_dialogexit" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>//没标题
<item name="android:background">#EBEBEB</item>//背景的颜色
<item name="android:windowIsFloating">true</item>//dialog窗口外的蒙版
<item name="android:windowFrame">@null</item>//无边框
<item name="android:windowBackground">@android:color/transparent</item>//透明效果
</style>
第二部:写dialog的view,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="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textview_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:gravity="center"
android:text="确认退出登录?"
android:textSize="16sp"
android:textColor="#000000"
android:background="#ffffff"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/button_cancle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="取消"
android:textColor="#0080C0"
android:textSize="16sp"
android:layout_marginRight="1.5dp"
android:background="@drawable/button_back_settting_noshow"/>
<Button
android:id="@+id/button_sure"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="确定"
android:textColor="#0080C0"
android:textSize="16sp"
android:layout_marginLeft="1.5dp"
android:background="@drawable/button_back_settting_noshow"/>
</LinearLayout>
</LinearLayout>
第三部:定义一个dialog可以直接写个类,也可以直接在dialog的申明后进行属性设置,由于需要的次数比较多,就封装成一个类。我在其中对dialog的大小及位置,进行了设置。首先得到dialog的window,getWindow(),在得到本地设备的长宽,在activity中使用getWindowManager();而自定义dialog不再,使用context.getSystemService(Cotext.WINDOW_SERVICE);得到windowManager,通过WindowManager.getDisplay()的得到display对象。在通过display.getSize(new Point).对Point进行设置大小,Point.x,的到宽,Point.Y得到屏幕的高。在通过之前得到的window得到params。在 mWindow.setAttributes(mParams);设置dialog的宽高。最后通过 mWindow.setGravity(Gravity.CENTER);设置位置,其中的参数可以自定义,设置dialog的具体位置。
public class DialogExit extends Dialog {
private Button mButtonSure;
private Button mButtonCancle;
private TextView mTextView;
private View mView;
private LayoutInflater mInflater;
private Window mWindow;
private WindowManager.LayoutParams mParams;
private WindowManager mManager;
public DialogExit(Context context,String content) {
super(context, R.style.style_dialogexit);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//得到layoutInflater
mView = mInflater.inflate(R.layout.dialogexit, null);
mButtonCancle = (Button) mView.findViewById(R.id.button_cancle);
mButtonSure = (Button) mView.findViewById(R.id.button_sure);
mTextView= (TextView) mView.findViewById(R.id.textview_content);
mTextView.setText(content);
super.setContentView(mView);
mWindow=this.getWindow();
mManager= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display=mManager.getDefaultDisplay();
Point point=new Point();
display.getSize(point);
mParams=mWindow.getAttributes();
mParams.height= (int) (point.y*0.3);
mParams.width= (int) (point.x*0.8);
mWindow.setAttributes(mParams);
mWindow.setGravity(Gravity.CENTER);
}
public void setOnSureListener(View.OnClickListener listener) {
mButtonSure.setOnClickListener(listener);
}
public void setOnCancleListener(View.OnClickListener listener) {
mButtonCancle.setOnClickListener(listener);
}
}