android – 自定义对话框大小匹配Theme.Holo.Light.Dialog

如果我有一个活动,它的主题设置为Theme.Holo.Light.Dialog,它将扩大.它几乎完全以纵向模式在手机上填充屏幕,但在风景模式下,它不会拉长不合时宜.例如,在Google的这张照片中,您可以看到不填写整个屏幕的对话框.

它不会崩溃以匹配标题的宽度,如果您有通过扩展Dialog类的类拥有自己的Dialog构建将会发生什么.

这是我的布局会发生什么.

什么属性我需要应用于LinearLayout使它变得漂亮?

解决方法

您可以使用Theme.Holo.Light.Dialog.MinWidth来正确调整布局大小.

从文档:

public static final int Theme_Holo_Light_Dialog_MinWidth

Variant of Theme.Holo.Light.Dialog that has a nice minimum width for a
regular dialog.

使用它的方法是通过将ContextThemeWrapper代替到您的自定义Dialog的构造函数的Context(使用这个):

YourCustomDialog cDialog = new YourCustomDialog(
                        new ContextThemeWrapper(this,android.R.style.Theme_Holo_Light_Dialog_MinWidth));

这是如何定义Theme.Holo.Light.Dialog.MinWidth:

<style name="Theme.Holo.Light.Dialog.MinWidth">
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
</style>

来自dimensions.xml:

@android:扪/ dialog_min_width_major:

<!-- The platform's desired minimum size for a dialog's width when it
     is along the major axis (that is the screen is landscape).  This may
     be either a fraction or a dimension. -->
<item type="dimen" name="dialog_min_width_major">65%</item>

@android:扪/ dialog_min_width_minor:

<!-- The platform's desired minimum size for a dialog's width when it
     is along the minor axis (that is the screen is portrait).  This may
     be either a fraction or a dimension. -->
<item type="dimen" name="dialog_min_width_minor">95%</item>

看起来,您发布的图片中的对话框的宽度大约为65%.在肖像模式下,这将是95%.

老实说,在纵向模式下,宽度看起来不像95%,但是比以前更好:):

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...