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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...