How to set margins to a custom dialog?

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

    1) change your main parent layout height match_content like below :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="match_content"
        android:layout_margin="50dip"   
        android:orientation="vertical"
        android:layout_gravity="top|center">
    

    2) now add style to your styles.xml

    <style name="dialogTheme" parent="Base.Theme.AppCompat.Light.Dialog.Alert"/>
    

    3) now add below code to show dialog :

    Dialog dialog = new Dialog(DayActivity.this, R.style.dialogTheme);
    dialog.setContentView(R.layout.dialog_custom_layout);
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    dialog.show();
    
    0 讨论(0)
  • 2020-12-05 06:49

    I had the same problem and I solved it by setting the window background on an InsetDrawable:

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    ...
    ...
    AlertDialog dialog = builder.create();
    
    ColorDrawable back = new ColorDrawable(Color.TRANSPARENT);
    InsetDrawable inset = new InsetDrawable(back, 20);
    dialog.getWindow().setBackgroundDrawable(inset);
    
    dialog.show();
    

    In this case the dialog will appear with a margin of 20 to all edges.

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

    One easy way is to set the root layout to transparent background. Then, have its first child have margins or pre set height and width.

    0 讨论(0)
提交回复
热议问题