问题
My custome style for AlertDialog look like:
<style name="Testing.Dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/color_accent</item>
<item name="android:textColorPrimary">@color/text_color_primary</item>
<item name="android:background">@color/color_primary</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
I need change width and height, because it is too large on my tablet. Any ideas?
Below code not working properly:
<item name="windowMinWidthMajor">@dimen/abc_dialog_min_width_major</item>
<item name="windowMinWidthMinor">@dimen/abc_dialog_min_width_minor</item>
回答1:
If it is a customizes dialog box then you can set the height and width in new created XML file only. but if you are using AlertDialog.builder then use this.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle("Title");
alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height.
And follow this Hope it may help you out.
回答2:
I was able to adjust the width like so:
<style name="NarrowDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="windowFixedWidthMajor">70%</item>
<item name="windowFixedWidthMinor">70%</item>
</style>
Dialog dialog = new AlertDialog.Builder(this, R.style.NarrowDialog)...
There is also a minimum width you might wish to adjust:
<item name="windowMinWidthMajor">65%</item>
<item name="windowMinWidthMinor">65%</item>
Attribute details:
windowFixedHeightMajor
:
A fixed height for the window along the major axis of the screen, that is, when in portrait.
windowFixedHeightMinor
:
A fixed height for the window along the minor axis of the screen, that is, when in landscape.
windowFixedWidthMajor
:
A fixed width for the window along the major axis of the screen, that is, when in landscape.
windowFixedWidthMinor
:
A fixed width for the window along the minor axis of the screen, that is, when in portrait.
回答3:
I found that an easier way is to set the style in styles.xml and then set android:windowMinWidthMajor and android:windowMinWidthMinor
<style name="Testing.Dialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowMinWidthMajor">90%</item>
<item name="android:windowMinWidthMinor">90%</item>
</style>
来源:https://stackoverflow.com/questions/30343999/alertdialog-appcompat-width-and-height