Android:Dialog占据100%的宽度

Vik*_*lla 1 android dialog android-layout

 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);
Run Code Online (Sandbox Code Playgroud)

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

Pav*_*vya 7

添加以下样式

<style name="Theme_Dialog" parent="android:Theme.Holo.Dialog">
        <item name="android:windowMinWidthMajor">100%</item>
        <item name="android:windowMinWidthMinor">100%</item>
</style> 
Run Code Online (Sandbox Code Playgroud)

final Dialog dialog = new Dialog(getActivity(), R.style.Theme_Dialog);
Run Code Online (Sandbox Code Playgroud)


Par*_*era 5

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

    //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);
Run Code Online (Sandbox Code Playgroud)