Android 控制自定义警报对话框在不同屏幕方向的尺寸
我想根据屏幕方向设置自定义警报对话框的尺寸。我的意图是交换高度和宽度值,以保持框的大小相同,同时处理各种设备的各种屏幕大小,由于Android碎片化,似乎很难在所有设备上实现相同的效果。Android自动调整大小对我来说似乎很奇怪 肖像画:Android 控制自定义警报对话框在不同屏幕方向的尺寸,android,android-alertdialog,screen-orientation,Android,Android Alertdialog,Screen Orientation,我想根据屏幕方向设置自定义警报对话框的尺寸。我的意图是交换高度和宽度值,以保持框的大小相同,同时处理各种设备的各种屏幕大小,由于Android碎片化,似乎很难在所有设备上实现相同的效果。Android自动调整大小对我来说似乎很奇怪 肖像画: alertDialog.width=screen.width*0.8 alertDialog.height=screen.height=0.5 alertDialog.width=screen.width*0.5; alertDialog.height=s
alertDialog.width=screen.width*0.8
alertDialog.height=screen.height=0.5
alertDialog.width=screen.width*0.5;
alertDialog.height=screen.height*0.8
景观:
alertDialog.width=screen.width*0.8
alertDialog.height=screen.height=0.5
alertDialog.width=screen.width*0.5;
alertDialog.height=screen.height*0.8
请注意,自定义警报对话框必须使用相同的代码,并支持从JellyBean(至少4.2)到Nougat(7)的Android版本
我正在使用android.support.v7.AlertDialog(最新的可用工具)
我认为现在应该避免使用android.app.Dialog(旧版)
此外,我需要一个黑色边框和白色背景相同。我无法在所有设备上实现相同的效果(我需要圆角的透明度)
我曾经使用过android:windowMinWidthMajor和android:windowMinWidthMinor,但它们会影响两种布局(纵向和横向),如果内容不符合指定的约束条件,它们似乎会被忽略
我希望有android:windowMaxWidthMinor&android:windowMaxWidthMajor这是我用来调整页面上特定视图大小的东西:
myView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
item.height = myView.getHeight();
});
将其应用于您想做的事情:
myView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
LayoutParams params = (get the layout params according to the layout);
params.width = myView.getWidth() * 0.5;
params.height = myView.getHeight() * 0.8;
myView.setLayoutParams(params);
});
您必须使用侦听器,因为对象将被添加到视图树中,将被测量并准备显示,但尚未显示,因此您可以在对象可见之前更改其尺寸。这是我用于调整页面上特定视图大小的工具:
myView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
item.height = myView.getHeight();
});
将其应用于您想做的事情:
myView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
LayoutParams params = (get the layout params according to the layout);
params.width = myView.getWidth() * 0.5;
params.height = myView.getHeight() * 0.8;
myView.setLayoutParams(params);
});
您必须使用侦听器,因为对象将被添加到视图树中,将被测量并准备显示,但尚未显示,因此您可以在对象可见之前更改其尺寸。更新:
alertDialog.width=screen.width*0.8
alertDialog.height=screen.height=0.5
alertDialog.width=screen.width*0.5;
alertDialog.height=screen.height*0.8
我可以通过为我的布局设置android:layout\u centerInParent=“true”
来实现它。和样式中每个构件的布局值
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
以实现中心对齐
XML代码可在
Java代码可在
我想知道这行代码是多余的
view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
因此,让Android系统根据内容决定尺寸和大小。它适用于我的情况,但在长文本的情况下会失败,也许某种滚动布局会有所帮助。干杯 更新:
alertDialog.width=screen.width*0.8
alertDialog.height=screen.height=0.5
alertDialog.width=screen.width*0.5;
alertDialog.height=screen.height*0.8
我可以通过为我的布局设置android:layout\u centerInParent=“true”
来实现它。和样式中每个构件的布局值
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
以实现中心对齐
XML代码可在
Java代码可在
我想知道这行代码是多余的
view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
因此,让Android系统根据内容决定尺寸和大小。它适用于我的情况,但在长文本的情况下会失败,也许某种滚动布局会有所帮助。干杯 您可能希望查看DialogTheme,因为它控制对话框的显示,包括透明阴影和白色背景。50%的宽度对于一个对话框来说是很窄的,但是Android会为你调整对话框的大小。最好的办法是更改主题并使用尺寸来控制宽度。或者,您必须等待对话框添加到视图中但尚未显示才能更改高度和宽度。您可能希望查看对话框主题,因为它控制对话框的显示,包括透明阴影和白色背景。50%的宽度对于一个对话框来说是很窄的,但是Android会为你调整对话框的大小。最好的办法是更改主题并使用尺寸来控制宽度。或者,您必须等待对话框添加到视图中但尚未显示,才能更改高度和宽度。