如何将边距设置为自定义对话框? [英] How to set margins to a custom dialog?

查看:139
本文介绍了如何将边距设置为自定义对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何将边距设置为自定义对话框?我问,因为我有一个自定义的对话框,但是当它显示它延伸来填充父,即使我在布局参数中明确地设置了WRAP_CONTENT。



基本上,对话框包含列表视图,其元素必须向下滚动,例如元素为1时,它不会拉伸,但是当添加更多项目时,对话框占据整个屏幕。



任何建议?我尝试了可能的解决方案的所有可能的组合,而没有取得满意的结果



编辑:添加对话框布局

 <?xml version =1.0encoding =utf-8?> 
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =fill_parent
android:layout_height =wrap_content
android:layout_margin =50dip
android:orientation =vertical
android:layout_gravity =top | center>

< FrameLayout android:layout_width =fill_parentandroid:layout_margin =5dipandroid:layout_height =wrap_content>

< TextView android:layout_width =wrap_contentandroid:layout_height =wrap_content
android:layout_gravity =center
android:textSize =20spandroid:textColor = @彩色/黑白/>

< Button android:layout_height =32dipandroid:layout_width =32dip
android:id =@ + id / guide_dialog_cross_button
android:background =@可拉伸/ button_cross_white/>

< / FrameLayout>


< ListView android:layout_width =fill_parentandroid:layout_height =wrap_content
android:fadingEdge =none
android:layout_margin =5dip />

< ImageButton android:layout_width =wrap_contentandroid:layout_height =wrap_content
android:layout_margin =5dip/>

< / LinearLayout>


解决方案

边距不适用于对话框,我想象顶级窗口视图不是支持边距的布局类型。我已经看到说定义为对话框样式(而不是顶级视图元素)的边距可以工作的帖子,但这似乎也不起作用。



您需要解决的问题是为对话框背景使用 inset drawable,并调整任何填充以考虑背景的额外插图。在下面的例子中,我刚刚设置左&



对话框背景drawable:

 <?xml version =1.0encoding =utf-8?> 
<! - drawable是对您的真实对话框背景的引用 - >
<! - insetRight和insetLeft添加边距 - >
< inset
xmlns:android =http://schemas.android.com/apk/res/android
android:drawable =@ drawable / dialog_background
android:insetRight =10dp
android:insetLeft =10dp>
< / inset>

对话主视图:

 <?xml version =1.0encoding =utf-8?> 
<! - paddingTop / padding对话框的填充填充 - >
<! - paddingLeft / paddingRight填充必须添加额外的插入空间来保持一致 - >
<! - 背景引用插图背景drawable - >
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
android:orientation =vertical
android:layout_width =fill_parent
android:layout_height =wrap_content
android:paddingTop =5dp
android:paddingBottom =5dp
android:paddingLeft =15dp
android: paddingRight =15dp
android:background =@ drawable / dialog_background_inset>

<! - ...其余的布局... - >

您可能还需要将Dialog本身的背景颜色设置为透明。添加如下颜色资源:

 < color name =transparent>#00000000< / color> 

并将对话框的窗口背景颜色设置为此(注意:您不能分配颜色直接,eclipse会抱怨)

 < style name =Dialogparent =android:style / Theme.Dialog> ; 
< item name =android:windowBackground> @ color / transparent< / item>
< item name =android:windowNoTitle> true< / item>
< item name =android:windowIsFloating> true< / item>
< / style>

此样式应该作为主题传递给Dialog的构造函数参数,如 new Dialog(context,R.style.Dialog);


Does anybody knows how can I set margins to a custom dialog? I'm asking because I've a custom dialog but when displayed it stretches to fill the parent, even though I set explicitly WRAP_CONTENT on the layout params.

Basically, the dialog contains a listview whose elements must be scrolled down, when the elements are 1 for example, it doesn't stretch, but when more items are added, then the dialog occupies the entire screen.

Any suggestions? I've trying all possible combinations of possible solutions without achieving satisfactory results

EDIT: Added the dialog layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="50dip"   
    android:orientation="vertical"
    android:layout_gravity="top|center">

    <FrameLayout android:layout_width="fill_parent" android:layout_margin="5dip" android:layout_height="wrap_content">

            <TextView android:layout_width="wrap_content"        android:layout_height="wrap_content" 
                android:layout_gravity="center"
                android:textSize="20sp" android:textColor="@color/black"/>

            <Button android:layout_height="32dip" android:layout_width="32dip" 
                android:id="@+id/guide_dialog_cross_button"
                android:background="@drawable/button_cross_white"/>

        </FrameLayout>


    <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:fadingEdge="none"
        android:layout_margin="5dip"/>

    <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_margin="5dip" />

</LinearLayout>

解决方案

Margins don't work for Dialogs, I imagine the top-level window view isn't a layout type that supports margins. I've seen posts saying margins will work when defined as the Dialog's style (rather than on the top-level view element), but this does not seem to work either.

What you need to do to work around the issue is to use an inset drawable for your Dialog background, and adjust any padding to account for the background's extra inset. In the example below, I'll just set left & right margins.

Dialog background drawable:

<?xml version="1.0" encoding="utf-8"?>
<!-- drawable is a reference to your 'real' dialog background -->
<!-- insetRight and insetLeft add the margins -->
<inset
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/dialog_background" 
    android:insetRight="10dp"
    android:insetLeft="10dp">
</inset>

Dialog main view:

<?xml version="1.0" encoding="utf-8"?>
<!-- paddingTop / paddingBottom padding for the dialog -->
<!-- paddingLeft / paddingRight padding must add the additional inset space to be consistent -->
<!-- background references the inset background drawable -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:background="@drawable/dialog_background_inset">

<!-- ...the rest of the layout... -->

You may also need to set the background colour of the Dialog itself to transparent. Add a colour resource like so:

<color name="transparent">#00000000</color>

And set the window background colour of the dialog to this (note: you can't assign the colour directly, eclipse will complain)

<style name="Dialog" parent="android:style/Theme.Dialog">
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
</style>

This style should be passed to your Dialog's constructor as the theme argument, as in new Dialog(context, R.style.Dialog);

这篇关于如何将边距设置为自定义对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆