2016-11-23 97 views
1
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:Dialog占据100%宽度

回答

1

试图改变这种

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

这个

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

'ViewGroup.LayoutParams.MATCH_PARENT'和'WindowManager.LayoutParams.MATCH_PARENT'都具有相同的恒定值,因此这将不会有任何效果 –

2

添加以下的风格

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

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

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

这种功效神奇,感谢您的 –

+0

欢迎,格莱德听到其帮助... –