Android get full width for custom Dialog

后端 未结 16 624
慢半拍i
慢半拍i 2020-12-02 15:19

in my application my created custom dialog dont have full height and i can not change and customize that.for example see this screen shot:

相关标签:
16条回答
  • 2020-12-02 15:37

    This can be considered as a simple hack to set your layout width for example 1000dp and height wrap content like :

    <LinearLayout
        android:layout_width="1000dp"
        android:gravity="center"
        android:padding="10dp"
        android:background="@color/colorAccent"
        android:orientation="vertical"     
        android:layout_height="wrap_content">
    
    0 讨论(0)
  • 2020-12-02 15:38

    In case anyone is wondering how to do it for a dialog shown using DialogFragment, you can override onCreateDialog to return a Dialog with custom style that has windowMinWidthMajor and minor set to 90%

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new Dialog(getActivity(), R.style.WideDialog);
    }
    

    Style:

    <style name="WideDialog" parent="Base.Theme.AppCompat.Dialog">
        <item name="android:windowMinWidthMajor">90%</item>
        <item name="android:windowMinWidthMinor">90%</item>
    </style>
    
    0 讨论(0)
  • 2020-12-02 15:40

    You don't need to add any style for that just try below one

      Dialog dialog = new Dialog(context);
      dialog.setContentView(R.layout.your_layout_here);
      dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      dialog.show();
    

    Note: using empty style is memory time consuming inside the processor. Instead directly use dailog.getWindow().setLayout(width,height).

    0 讨论(0)
  • 2020-12-02 15:41
    MyDialogFragment myDialogFragment = 
        new MyDialogFragment(activity,arraylist.get(position).getImage());
                                    myDialogFragment.show();
    
    Window window = myDialogFragment.getWindow();
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    
    0 讨论(0)
  • 2020-12-02 15:44

    Just try to wrap your dialog layout in a RelativeLayout

    0 讨论(0)
  • 2020-12-02 15:50

    For full width dialog you can create custom style for dialog. Code is given below for full width dialog:

    <style name="Custom_Dialog" parent="ThemeOverlay.AppCompat.Light" >
        <item name="windowMinWidthMajor">100%</item>
        <item name="windowMinWidthMinor">65%</item>
    </style>
    

    To assign this style to the dialog's constructor, add this to onCreate() method right after setContentView():

    getWindow()
        .setLayout(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        );
    
    0 讨论(0)
提交回复
热议问题