Android:Dialog占据100%的宽度

时间:2016-11-23 04:44:13

标签: android android-layout dialog

 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%。

5 个答案:

答案 0 :(得分:6)

添加以下样式

<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);

答案 1 :(得分:4)

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

    //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);

答案 2 :(得分:2)

尝试更改此

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

到这个

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

答案 3 :(得分:1)

首先,在自定义对话框的xml文件中,将RelativeLayout用作自定义对话框的父视图,然后将width设置为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));                 

答案 4 :(得分:0)

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