2016-11-23 80 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

歡迎,格萊德聽到其幫助... –