自定义的dialog

在网上搜索dialog,AlertDialog:警告对话框,使用最广泛功能最丰富的一个对话框。ProgressDialog:进度条对话框,只是对进度条进行了简单的封装。

DatePickerDialog:日期对话框。TimePickerDialog:时间对话框。大概也就这些类型的dialog,往往是满足不了我们的需求的,而且在美观上面也是难以达到UI的需求,因此我就想到了自己定义一个dialog,只需改变布局就可以轻松解决dialog的变换。

直接上代码:

LinearLayout.LayoutParams pm =newLinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

AlertDialog.Builder builder =newAlertDialog.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);

finalAlertDialog mAlertDialog = builder.create();

mAlertDialog.show();

mAlertDialog.getWindow().setContentView(view, pm);

接下来就是对于设置的按钮的点击的反应:比如

cancle.setOnClickListener(newView.OnClickListener() {

@Override

publicvoidonClick(View v) {

mAlertDialog.dismiss();

}

});

下面还是将布局文件粘贴进来吧:


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">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true">

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"/>

android:id="@+id/line2"

android:layout_width="260dp"

android:layout_height="1px"

android:layout_below="@+id/text1"

android:layout_centerHorizontal="true"

android:background="#666666"/>

android:layout_width="260dp"

android:layout_height="40dp"

android:layout_below="@id/line2"

android:layout_centerHorizontal="true"

android:background="#ffffff"

android:orientation="horizontal">

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"/>

android:layout_width="1px"

android:layout_height="40dp"

android:background="#666666"/>

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"/>

是不是非常简单?接下来就是运用到页面当中,只要稍作修改布局文件就可以实现想要的dialog的变换了。

csdn项目地址:http://blog.csdn.net/greatdaocaoren/article/details/54861123

你可能感兴趣的:(自定义的dialog)