Android 自定义dialog 设置宽度的问题

自定义弹框效果图

在这里插入图片描述

首先在自定义布局文件中设置了宽度,但是不起作用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
	android:background="@drawable/bg_dialog"
    >
<!--@drawable/bg_dialog -->
<!--@dimen/y460 设置宽度不起作用 -->
	<RelativeLayout
		android:layout_width="@dimen/y460"
		android:layout_height="@dimen/x200"
		android:background="@drawable/bg_dialog"
		android:gravity="center"
		android:layout_marginTop="@dimen/x19">

		<TextView
			android:id="@+id/dialog_Msg"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:gravity="center"
			android:text="确定删除吗?"
			android:textColor="@color/black"
			android:textSize="@dimen/y30"
			style="@style/sp_title_text" />

		<LinearLayout
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:layout_below="@id/dialog_Msg"
			android:layout_marginTop="@dimen/x50"
			android:layout_marginBottom="@dimen/x10"
			android:gravity="center">

			<Button
				android:layout_toRightOf="@+id/msg_sure"
				android:id="@+id/msg_cancel"
				android:layout_gravity="center"
				android:layout_width="@dimen/y180"
				android:layout_height="@dimen/x55"
				android:nextFocusRight="@+id/msg_cancel"
				android:nextFocusLeft="@+id/msg_cancel"
				android:background="@drawable/bg_button"
				android:textColor="#222222"
				android:text="@string/cancle"
				android:textSize="@dimen/y30"
				android:gravity="center"
				style="@style/sp_title_text" />

				<Button
					android:id="@+id/msg_sure"
					android:layout_marginLeft="@dimen/y40"
					android:layout_gravity="center"
					android:layout_width="@dimen/y180"
					android:layout_height="@dimen/x55"
					android:nextFocusRight="@+id/msg_sure"
					android:nextFocusLeft="@+id/msg_sure"
					android:background="@drawable/bg_button"
					android:textColor="#FF0000"
					android:text="@string/sure"
					android:textSize="@dimen/y30"
					android:gravity="center"
					style="@style/sp_title_text"/>

		</LinearLayout>
	</RelativeLayout>
</RelativeLayout>

创建 dialog 引用布局文件

private void showDelete(final FileDownloaderModel model){
        final Dialog dlg = new Dialog(this, R.style.Theme_dialog);

        View view = LayoutInflater.from(this).inflate(R.layout.login_msg, null);
        TextView msgView = (TextView) view.findViewById(R.id.dialog_Msg);
        if(model == null){
            msgView.setText("是否删除全部课本?");//mContext.getString(R.string.down_msg)
        }else {
            msgView.setText("是否删除课本?");//mContext.getString(R.string.down_msg)
        }

        Button mSure = (Button) view.findViewById(R.id.msg_sure);
        Button mCancel = (Button) view.findViewById(R.id.msg_cancel);
        mSure.requestFocus();
        mSure.setBackgroundResource(R.drawable.list_item_selector);
        mCancel.setBackgroundResource(R.drawable.list_item_selector);

        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dlg.cancel();
            }
        });
        mSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //如果等于空就删除全部

                dlg.cancel();
                complete_listView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                    	//刷新页面
                        refreshDownloaded();
                    }
                }, 1000);
            }
        });
        dlg.setContentView(view);
        dlg.show();
    }

解决设置宽度无效的问题

修改 dialog 的样式文件 R.style.Theme_dialog

<style name="Theme_dialog"  parent="@android:style/Theme.DeviceDefault.Light.Dialog">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <!--设置横屏最少宽度-->
        <item name="android:windowMinWidthMajor">@dimen/y460</item>
    </style>

注意:
android:windowMinWidthMajor 是:
当平台沿主轴(即屏幕为横向)时,平台所需的最小尺寸为对话框的宽度。这可以是分数或维度。

android:windowMinWidthMinor是:
当平台沿着短轴(即屏幕是纵向)时,平台所需的最小尺寸为对话框的宽度。这可以是分数或维度。

在acticity 设置 dialog 的宽高

WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.height = getResources().getDimensionPixelOffset(R.dimen.x160); //设置高度
        lp.width = getResources().getDimensionPixelOffset(R.dimen.y350); //设置宽度

        dialog.getWindow().setAttributes(lp);

最新使用Dialog时,发现在以下低版本系统中会出现Dialog弹窗在左上角显示的情况,搜索了一下,发现了这个方法,记录一下。

 //dialog 居中显示
 Window window = dialog.getWindow();
 if (dialog != null && window != null) {
 WindowManager.LayoutParams attr = window.getAttributes();
 if (attr != null) {
 	attr.height = ViewGroup.LayoutParams.WRAP_CONTENT;
    attr.width = ViewGroup.LayoutParams.WRAP_CONTENT;
   	attr.gravity = Gravity.CENTER;//设置dialog 在布局中的位置
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值