2011-05-26 220 views
4

我有一个使用我在XML文件中定义的布局的自定义样式的对话框。我希望这个对话框以纵向模式填充屏幕的宽度,但只有横向模式下的大约400dip。 MaxWidth似乎是完美的方式,但我不知道如何将MaxWidth分配给对话框样式或XML布局。如何设置Android对话框的MaxWidth?

回答

-2

你试过设置android:minWidthandroid:minHeight在您的自定义XML

+15

不,但我不明白minWidth和minHeight如何帮助我设置最大宽度。 – chefgon 2011-05-27 16:16:15

3

假设你使用一个叫做layout/dialog_core.xml未来布局文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" > 

    //All your dialog views go here.... 

</LinearLayout> 

然后,您可以创建两个文件:layout/dialog.xmllayout-land/dialog.xml

layout/dialog.xml文件不会限制宽度:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" > 

    <include layout="@layout.dialog_core.xml" /> 

</LinearLayout> 

当你layout-land/dialog.xml应该有宽度的限制:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:maxWidth="400dp" > 

    <include layout="@layout.dialog_core.xml" /> 

</LinearLayout> 

当然,你现在需要在代码中使用R.layout.dialog布局。

P.S.我仅以举例的目的使用LinearLayout。任何布局视图都可以。

+0

我试过了,但android:maxWidth没有出现在LinearLayout的自动完成提议窗口中,所以我认为我不能使用它。无论如何,我尝试着把它放到项目中,但是这个属性没有效果,对话框仍然延伸到屏幕上。 – chefgon 2011-05-26 18:52:20

+0

@chefgon'android:maxWidth'不是你正在寻找的。你想要的是在纵向模式下android:layout_width =“fill_parent”。按照inazaruk的说法,不要忘记将'dialog-land.xml'放在布局文件夹'res/layout-land'中。 Android将根据屏幕方向自动加载正确的布局('dialog.xml'或'dialog-land.xml')。 – 2011-07-07 20:03:09

+0

据我所知,根据开发文档,'dialog-land.xml'也应该命名为'dialog.xml',但放置在@ resur说的'res/layout-land'中 - 我相信@inazaruk在他的解释中对他们进行了不同的命名。 – ataulm 2012-04-08 18:36:57

0

你可以在这里找到一个实际的解决方案:setting a max width for my dialog

答案是使用对话的主题活动,但考虑到它是基于Window对象,它应该不会太复杂,以适应它的对话框。

相关问题