在网上搜索dialog,AlertDialog:警告对话框,使用最广泛功能最丰富的一个对话框。ProgressDialog:进度条对话框,只是对进度条进行了简单的封装。
- DatePickerDialog:日期对话框。TimePickerDialog:时间对话框。大概也就这些类型的dialog,往往是满足不了我们的需求的,而且在美观上面也是难以达到UI的需求,因此我就想到了自己定义一个dialog,只需改变布局就可以轻松解决dialog的变换。
- 直接上代码:
-
接下来就是对于设置的按钮的点击的反应:比如LinearLayout.LayoutParams pm = new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); AlertDialog.Builder builder = new AlertDialog.Builder(xx.this); LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.dialog_yesorno, null);//这里的R.layout.alertdialog即为你自定义的布局文件 text1 = (TextView) view.findViewById(R.id.text1); text1.setText(msg); yes = (TextView) view.findViewById(R.id.yes); cancle = (TextView) view.findViewById(R.id.cancle); final AlertDialog mAlertDialog = builder.create(); mAlertDialog.show(); mAlertDialog.getWindow().setContentView(view, pm);
下面还是将布局文件粘贴进来吧:cancle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mAlertDialog.dismiss(); } });
-
<?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:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_centerInParent="true" android:background="#00000000"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"> <TextView android:id="@+id/text1" android:layout_width="260dp" android:layout_height="40dp" android:layout_centerHorizontal="true" android:background="@drawable/dialog_topradius" android:gravity="center" android:text="您确定吗?" android:textColor="#16a5af" android:textSize="16sp" /> <View android:id="@+id/line2" android:layout_width="260dp" android:layout_height="1px" android:layout_below="@+id/text1" android:layout_centerHorizontal="true" android:background="#666666" /> <LinearLayout android:layout_width="260dp" android:layout_height="40dp" android:layout_below="@id/line2" android:layout_centerHorizontal="true" android:background="#ffffff" android:orientation="horizontal"> <TextView android:id="@+id/yes" android:layout_width="130dp" android:layout_height="40dp" android:layout_gravity="center" android:background="@drawable/dialog_left" android:gravity="center" android:text="确定" android:textColor="#16a5af" android:textSize="16sp" /> <View android:layout_width="1px" android:layout_height="40dp" android:background="#666666" /> <TextView android:id="@+id/cancle" android:layout_width="130dp" android:layout_height="40dp" android:background="@drawable/dialog_right" android:gravity="center" android:text="取消" android:textColor="#16a5af" android:textSize="16sp" /> </LinearLayout> </RelativeLayout> </RelativeLayout>
是不是非常简单?接下来就是运用到页面当中,只要稍作修改布局文件就可以实现想要的dialog的变换了。