参考:Android 自定义dialog 设置宽度的问题_onlySound的博客-优快云博客_android dialog宽度
自定义dialog的布局文件dialog_del_fav:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_del_fav"
android:paddingStart="23mm"
android:paddingEnd="23mm"
android:paddingBottom="13mm">
<TextView
android:id="@+id/del_fav_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="25mm"
android:text="确认取消收藏吗?"
android:textColor="#fff0f0f0"
android:textSize="18mm" />
<TextView
android:id="@+id/tv_tv_del_fav_cancle"
android:layout_width="80mm"
android:layout_height="32mm"
android:layout_below="@id/del_fav_title"
android:layout_marginTop="25mm"
android:background="@drawable/bg_del_fav_slt"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:text="取消"
android:textColor="@color/white_F0F0F0"
android:textSize="18mm"
/>
<TextView
android:id="@+id/tv_del_fav_confirm"
android:layout_width="80mm"
android:layout_height="32mm"
android:layout_below="@id/del_fav_title"
android:layout_marginLeft="15mm"
android:layout_marginTop="25mm"
android:layout_toRightOf="@id/tv_tv_del_fav_cancle"
android:background="@drawable/bg_del_fav_slt"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:text="确认"
android:textColor="@color/white_F0F0F0"
android:textSize="18mm" />
</RelativeLayout>
activity引用:
private void exit() {
//初始化并弹出对话框方法
View view = View.inflate(mContext, R.layout.dialog_del_fav, null); // 自定义
// View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_del_fav, null, false);
final AlertDialog dialog = new AlertDialog.Builder(mContext).setView(view).create();
TextView title = view.findViewById(R.id.del_fav_title);
TextView cancel = view.findViewById(R.id.tv_tv_del_fav_cancle);
TextView confirm = view.findViewById(R.id.tv_del_fav_confirm);
title.setText("确认退出吗");
cancel.requestFocus();
cancel.requestFocusFromTouch();
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
App.getInstance().exitApp();
dialog.dismiss();
}
});
dialog.show();
}
起初的效果图:

明显宽不对
然后改了一下 ,在dialog.show();后面加了两句代码:
private void exit() {
//初始化并弹出对话框方法
View view = View.inflate(mContext, R.layout.dialog_del_fav, null); // 自定义
// View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_del_fav, null, false);
final AlertDialog dialog = new AlertDialog.Builder(mContext).setView(view).create();
TextView title = view.findViewById(R.id.del_fav_title);
TextView cancel = view.findViewById(R.id.tv_tv_del_fav_cancle);
TextView confirm = view.findViewById(R.id.tv_del_fav_confirm);
title.setText("确认退出吗");
cancel.requestFocus();
cancel.requestFocusFromTouch();
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
App.getInstance().exitApp();
dialog.dismiss();
}
});
dialog.show();
//此处设置位置窗体大小,注意一定要在show方法调用后再写设置窗口大小的代码,否则不起效果会
dialog.getWindow().setLayout(290, 183);
dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_del_fav));
}
虽然显示正常了,但是由于项目在很多分辨率的机器上运行,有的机器上显示不全了,于是想别的办法,给dialog加style,将AlertDialog.Builder(mContext).setView(view).create();修改为:AlertDialog.Builder(mContext,R.style.Theme_dialog).setView(view).create();
整体修改如下:
private void exit() {
//初始化并弹出对话框方法
View view = View.inflate(mContext, R.layout.dialog_del_fav, null); // 自定义
// View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_del_fav, null, false);
final AlertDialog dialog = new AlertDialog.Builder(mContext,R.style.Theme_dialog).setView(view).create();
TextView title = view.findViewById(R.id.del_fav_title);
TextView cancel = view.findViewById(R.id.tv_tv_del_fav_cancle);
TextView confirm = view.findViewById(R.id.tv_del_fav_confirm);
title.setText("确认退出吗");
cancel.requestFocus();
cancel.requestFocusFromTouch();
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
App.getInstance().exitApp();
dialog.dismiss();
}
});
dialog.show();
//此处设置位置窗体大小,注意一定要在show方法调用后再写设置窗口大小的代码,否则不起效果会
// dialog.getWindow().setLayout(290, 183);
// dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_del_fav));
}
其中,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">220mm</item>
</style>
修改后的效果:

本文介绍了在Android开发中遇到自定义Dialog布局设置宽高无效的问题,通过添加额外代码调整以及使用自定义style实现了适配不同分辨率设备的需求。详细步骤包括在dialog.show()后添加调整宽高的代码以及创建自定义style文件来解决问题。
4322

被折叠的 条评论
为什么被折叠?



