How to set margins to a custom dialog?

前端 未结 15 1215
再見小時候
再見小時候 2020-12-05 05:56

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

15条回答
  •  清歌不尽
    2020-12-05 06:43

    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:

    
    
    
    
    
    

    Dialog main view:

    
    
    
    
    
    
    
    

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

    #00000000
    

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

    
    

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

提交回复
热议问题