我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。
我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。
当前回答
下面的方法对我来说效果不错:
<style name="MyAlertDialogTheme" parent="Base.Theme.AppCompat.Light.Dialog.Alert">
<item name="windowFixedWidthMajor">90%</item>
<item name="windowFixedWidthMinor">90%</item>
</style>
(注:windowMinWidthMajor/Minor,如之前的答案所建议的,没有做到这一点。我的对话框不断改变大小取决于内容)
然后:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogTheme);
其他回答
下面的方法对我来说效果不错:
<style name="MyAlertDialogTheme" parent="Base.Theme.AppCompat.Light.Dialog.Alert">
<item name="windowFixedWidthMajor">90%</item>
<item name="windowFixedWidthMinor">90%</item>
</style>
(注:windowMinWidthMajor/Minor,如之前的答案所建议的,没有做到这一点。我的对话框不断改变大小取决于内容)
然后:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogTheme);
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
只要给AlertDialog这个主题
<style name="DialogTheme" parent="Theme.MaterialComponents.Light.Dialog.MinWidth">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
</style>
如果你正在使用约束布局,你可以在其中设置任何视图,以填充屏幕的百分比:
layout_constraintWidth_percent = " 0.8 "
例如,如果你在对话框中有一个ScrollView你想把它设置为屏幕高度的一个百分比。它是这样的:
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.8">
希望它能帮助到一些人!!
你必须在显示之前设置对话框的高度和宽度(dialog.show())
所以,像这样做:
dialog.getWindow().setLayout(width, height);
//then
dialog.show()
得到这段代码,我做了一些改变:
dialog.getWindow().setLayout((int)(MapGeaGtaxiActivity.this.getWindow().peekDecorView().getWidth()*0.9),(int) (MapGeaGtaxiActivity.this.getWindow().peekDecorView().getHeight()*0.9));
但是,当设备改变位置时,对话框的大小也会改变。当度量发生变化时,您可能需要自己处理。 peekDecorView,暗示在活动中的布局是正确初始化的,否则你可以使用
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int wwidth = metrics.widthPixels;
为了得到屏幕大小