How to set margins to a custom dialog?

前端 未结 15 1196
再見小時候
再見小時候 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:31

    A simple solution that worked for me was to wrap my entire View in another one, and set the margin to which is now the inner View.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <LinearLayout
            android:layout_margin="22dp"
            android:orientation="vertical"
            android:layout_width="220dp"
            android:layout_height="wrap_content">
    
        ... Buttons, TextViews, etc
    
        </LinearLayout>
    
    </LinearLayout>
    

    Obs: that worked when I wanted margins within my AlertDialog. Not regarding the screen.

    0 讨论(0)
  • 2020-12-05 06:32

    you could make a new shape drawable resource and set color of it transparent :

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#00000000"/>
    </shape>
    

    then you can add these simple lines in your dialog class onCreate method :

    getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);  
    getWindow().setBackgroundDrawableResource(R.drawable.drawble_resource_you_made_before);
    

    don't forget to put your dialog layout resource background white or any color you want then you could easily by editing your dialog layout resource and customize it :)

    0 讨论(0)
  • 2020-12-05 06:35

    if you extend Dialog Fragment, put this:

    override fun onStart() {
            super.onStart()
            if (dialog != null) {
                dialog!!.getWindow()!!.setLayout(Constraints.LayoutParams.MATCH_PARENT, Constraints.LayoutParams.WRAP_CONTENT) // full width dialog
                val back = ColorDrawable(Color.TRANSPARENT)
                val margin = 16
                val inset = InsetDrawable(back, margin)
                dialog!!.window!!.setBackgroundDrawable(inset)
            }
        }
    

    layoutparams is depend on your dialog parent view

    0 讨论(0)
  • 2020-12-05 06:35

    Step 1. Add the following styles

        <style name="CustomStyle" parent="Theme.MaterialComponents.Light.Dialog">
                <item name="android:windowIsFloating">true</item>
                <item name="android:windowIsTranslucent">false</item>
                <item name="android:windowTranslucentStatus">false</item>
                <item name="android:windowTranslucentNavigation">false</item>
            </style>
    

    Step 2. Extend custom dialog from DialogFragment and override the following

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setStyle(STYLE_NO_TITLE, R.style.CustomStyle)
        }
    
        override fun onStart() {
            super.onStart()
            val dialog: Dialog? = dialog
            if (dialog != null) {
                val width = ViewGroup.LayoutParams.MATCH_PARENT
                val height = ViewGroup.LayoutParams.MATCH_PARENT
                dialog.window?.setLayout(width, height)
                dialog?.window?.setBackgroundDrawable(InsetDrawable(ColorDrawable(resources.getColor(R.color.transparent, null)), 10, 10, 10, 10))
            }
        }
    

    You will be able to get full screen dialog fragment with margins, without the status bar or overlapping navigation icons.

    0 讨论(0)
  • 2020-12-05 06:37

    There is a much easier and cleaner solution than any of the existing answers:

    val dialog = AlertDialog.Builder(context)
        .setTitle(title)
        .create()
    
    dialog.setView(view, spacingLeft, spacingTop, spacingRight, spacingBottom)
    

    https://developer.android.com/reference/android/app/AlertDialog#setView(android.view.View,%20int,%20int,%20int,%20int)

    Note that AlertDialog.Builder does not have this method, but you have to call it on the dialog directly.

    (Setting margins does not work because AlertController overrides LayoutParams when adding the view to the parent FrameLayout)

    0 讨论(0)
  • 2020-12-05 06:38

    If you are creating a custom dialog with the background you can prefer the following code. You can change window property MATCH_PARENT for full screen.

    My issue was width of dialog matching to its parent without margin

    try following code as per requirement

        Window window = event_dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        wlp.flags &= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        window.setAttributes(wlp);
    

    set these property before dialog.show() OR you can set LEFT,RIGHT,TOP,BOTTOM margin to your dialog by setting margin to your root layout and custom background to its child layout like

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">
       <LinearLayout
        android:id="@+id/customLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/card_round_corner"
        android:orientation="vertical">
    
       </LinearLayout>
    </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题