随记3——自定义Dialog的宽度


女神镇楼
在开发的过程中,大多数时候写Dialog都是默认的宽度,但是有时候默认宽度不是很合适的时候,这时候就需要自己去定义Dialog的宽度了。
先看一下效果:

下面上代码:
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:这篇是补上周的。欢迎交流。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值