如何设置页边距到定制对话框?对话框、如何设置、页边距到

2023-09-04 23:18:24 作者:时光不能倒流

有谁知道我怎么可以设置页边距为自定义对话框?我这么问是因为我有一个自定义对话框,但显示时,它拉伸以填充父,即使我明确设置WRAP_CONTENT的布局PARAMS。

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

有什么建议?我想可能的解决方案,所有可能的组合没有取得令人满意的结果。

编辑:添加对话框布局

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =50dip
    机器人:方向=垂直
    机器人:layout_gravity =顶|中心>

    <的FrameLayout机器人:layout_width =FILL_PARENT机器人:layout_margin =5dip机器人:layout_height =WRAP_CONTENT>

            < TextView的Andr​​oid的:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
                机器人:layout_gravity =中心
                机器人:TEXTSIZE =20SP机器人:文字颜色=@彩色/黑白/>

            <按钮机器人:layout_height =32dip机器人:layout_width =32dip
                机器人:ID =@ + ID / guide_dialog_cross_button
                机器人:背景=@可绘制/ button_cross_white/>

        < /的FrameLayout>


    < ListView的机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT
        机器人:fadingEdge =无
        机器人:layout_margin =5dip/>

    < ImageButton的机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT
        机器人:layout_margin =5dip/>

< / LinearLayout中>
 

解决方案

页边距没有为对话框工作,我想象中的顶层窗口的观点是不支持的利润率布局类型。我看到帖子说,当定义对话框的方式(而不是顶层视图元素)的利润率将工作,但这似乎并没有擦出火花。

wps怎么设置页面大小和方向 设置大小的方法

您需要做什么来解决这个问题是使用插入绘制你的对话框的背景,并调整任何填充设置为背景的额外的插图。在下面的例子中,我将只设置左和放大器;右页边距。

对话背景绘制:

 < XML版本=1.0编码=UTF-8&GT?;
<插图
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    <  - !引用您的真正的对话背景 - >
    机器人:可绘制=@可绘制/ dialog_background
    <! - 增加利润 - >
    机器人:insetRight =10dp
    机器人:insetLeft =10dp>
< /插图>
 

对话主要观点:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    <! - 顶部/底部填充的对话 - >
    机器人:paddingTop =5DP
    机器人:paddingBottom会=5DP
    <! - 左/右填充必须添加额外的插图空间是一致的 - >
    机器人:以下属性来=15dp
    机器人:paddingRight =15dp
    &所述;! - 参照插图背景​​可拉伸 - >
    机器人:背景=@可绘制/ dialog_background_inset>

&所述;! -  ...布局的其余...  - >
 

您可能还需要设置对话框本身为透明的背景色。添加颜色资源,如下所示:

 <颜色名称=透明>#00000000< /彩色>
 

和设置对话框的窗口背景色为这个(注:不能直接指定颜色,日食会抱怨)

 <样式名称=对话框父=机器人:款式/ Theme.Dialog>
    <项目名称=机器人:windowBackground> @色/透明< /项目>
    <项目名称=机器人:windowNoTitle>真< /项目>
    <项目名称=机器人:windowIsFloating>真< /项目>
< /风格>
 

这种风格应该传递给你的对话框的构造函数的主题参数,如新的对话框(背景下,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"?>
<inset
    xmlns:android="http://schemas.android.com/apk/res/android"
    <!-- a reference to your 'real' dialog background -->
    android:drawable="@drawable/dialog_background" 
    <!-- add the margins -->
    android:insetRight="10dp"
    android:insetLeft="10dp">
</inset>

Dialog main view:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    <!-- top / bottom padding for the dialog -->
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    <!-- left / right padding must add the additional inset space to be consistent -->
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    <!-- reference to the inset background drawable -->
    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);