【问题标题】:Android : Dialog occupy 100% widthAndroid:对话框占据 100% 宽度
【发布时间】:2016-11-23 04:44:13
【问题描述】:
 final Dialog dialog = new Dialog(this, R.style.theme_dialog);
 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
 dialog.setContentView(R.layout.dialog_name);
 dialog.setCancelable(false);

 dialog.getWindow().setGravity(Gravity.TOP);
 dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

上面的代码占据大约 90% 的宽度,但我想要 100%。

【问题讨论】:

    标签: android android-layout dialog


    【解决方案1】:

    添加以下样式

    <style name="Theme_Dialog" parent="android:Theme.Holo.Dialog">
            <item name="android:windowMinWidthMajor">100%</item>
            <item name="android:windowMinWidthMinor">100%</item>
    </style> 
    

    final Dialog dialog = new Dialog(getActivity(), R.style.Theme_Dialog);
    

    【讨论】:

    • 未达到全宽
    • 我需要一个使用 DialogFragment 的全宽对话框。当我应用此解决方案时,它不起作用
    • @AbrahamMathew 它只用于 Dialog 不用于 DialogFragment
    • 正如@mrudul-tora 提到的,使用dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    【解决方案2】:

    尝试以下代码,这将解决您的问题。

        //Grab the window of the dialog, and change the width
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        Window window = dialog.getWindow();
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        lp.copyFrom(window.getAttributes());
        //This makes the dialog take up the full width
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
    

    【讨论】:

      【解决方案3】:

      尝试改变这一点

       dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      

      到这里

       dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                      WindowManager.LayoutParams.MATCH_PARENT);
      

      【讨论】:

      • ViewGroup.LayoutParams.MATCH_PARENTWindowManager.LayoutParams.MATCH_PARENT 都具有相同的常数值,所以这不会有任何影响
      • 第二个参数是高度,它不影响对话框的宽度。是的,它不能解决问题。
      【解决方案4】:

      首先,使用RelativeLayout作为自定义对话框的父视图,然后在自定义对话框的xml文件中设置宽度为match_parent,高度为wrap_content。 然后在java文件中使用这段代码...

      Dialog dialog=new Dialog(AllProductListActivity.this);
      dialog.setContentView(R.layout.your_custom_dialog_layout);                
      dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));             
      dialog.getWindow().setLayout.(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      dialog.show();    
      

      您将阅读许多仅使用 setLayout() 的解决方案,仅此一项无法使对话框的宽度与 match_parent 匹配。但是在这里,添加以下行时,对话框的宽度将为 match_parent 而高度将为 wrap_content。

      dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));                 
      

      【讨论】:

      • 这个解决方案对我有用。诀窍是使用setBackgroundDrawable
      【解决方案5】:

      尝试使用 RelativeLayout 作为对话框的根视图。

      【讨论】:

        猜你喜欢
        • 2011-02-07
        • 1970-01-01
        • 2019-05-17
        • 1970-01-01
        • 2012-04-23
        • 2013-02-14
        • 2022-01-25
        • 2014-02-03
        • 2015-04-27
        相关资源
        最近更新 更多