女神镇楼
在开发的过程中,大多数时候写Dialog都是默认的宽度,但是有时候默认宽度不是很合适的时候,这时候就需要自己去定义Dialog的宽度了。
先看一下效果:
下面上代码:
DialogAct.java
dialog_show.xml
颜色什么的自己随意定义就好。
下面就是最主要的了
AlertDialogUtil.java
下面上代码:
DialogAct.java
public class DialogAct extends AppCompatActivity {
Context mContext;
final int MSG_CANCEL = 0, MSG_SURE = 1;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case MSG_CANCEL:
Toast.makeText(mContext, "你点了取消", Toast.LENGTH_SHORT).show();
break;
case MSG_SURE:
Toast.makeText(mContext, "你点了确定", Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
mContext = this;
}
public void showDialog(View view){
switch (view.getId()){
case R.id.btn_1:
//默认的Dialog
AlertDialogUtil.showCouponDialog(mContext, -1, mHandler, MSG_CANCEL, MSG_SURE);
break;
case R.id.btn_2:
//0.8
AlertDialogUtil.showCouponDialog(mContext, 0.8f, mHandler, MSG_CANCEL, MSG_SURE);
break;
case R.id.btn_3:
//0.6
AlertDialogUtil.showCouponDialog(mContext, 0.6f, mHandler, MSG_CANCEL, MSG_SURE);
break;
}
}
}
dialog_show.xml
<?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">
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showDialog"
android:text="默认Dialog" />
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showDialog"
android:text="屏幕0.8倍的Dialog" />
<Button
android:id="@+id/btn_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showDialog"
android:text="屏幕0.6倍的Dialog" />
</LinearLayout>
颜色什么的自己随意定义就好。
下面就是最主要的了
AlertDialogUtil.java
public class AlertDialogUtil {
/**
* 显示弹窗
*/
public static void showCouponDialog(final Context context, float scale, final Handler handler, final int MsgWhatCancel, final int MsgWhatSure){
final AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.show();
View view = LayoutInflater.from(context).inflate(R.layout.dialog_show, null);
Window window = dialog.getWindow();
window.setContentView(view);
window.setBackgroundDrawable(new ColorDrawable(0));
//设置dialog的宽度
if (scale < 0 || scale >= 1){
// 默认dialog
}else if (scale > 0 && scale < 1){
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = (int)(CommonUtil.getScreenWidth(context) * scale);
window.setAttributes(lp);
}
dialog.setCanceledOnTouchOutside(false);
TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
TextView tv_ok = (TextView) view.findViewById(R.id.tv_ok);
TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
//应该可以和上面的判断结合
if (scale < 0 || scale >= 1){
// 默认dialog
tv_content.setText("默认的Dialog");
}else if (scale > 0 && scale < 1){
tv_content.setText(scale + "倍的Dialog");
}
tv_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (handler != null) {
handler.sendEmptyMessage(MsgWhatCancel);
}
dialog.dismiss();
}
});
tv_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (handler != null) {
Message msg = handler.obtainMessage();
msg.what = MsgWhatSure;
handler.sendMessage(msg);
}
dialog.dismiss();
}
});
}
}
这样就可以根据自己的要求自定义Dialog的宽度了。PS:这篇是补上周的。欢迎交流。