alertdialog正/负按钮在全屏对话框中不匹配父级

问题描述 投票:1回答:1

在我的dialogfragment类的OnCreateDialog中我这样做:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        inflater=getActivity().getLayoutInflater();
        v=inflater.inflate(R.layout.new_spending_fragment_layout,null);
        builder.setPositiveButton("Fire", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // FIRE ZE MISSILES!
                    }
                });
        builder.setView(v);
        return builder.create();

在dialogfragment类的OnStart中,我正在对话框片段进行全屏显示

Dialog d = getDialog();
        if (d!=null){
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            d.getWindow().setLayout(width, height);
        }

我希望alertdialog中的正面按钮(Fire)显示在底部。

模拟器预览:

enter image description here

我正在膨胀的布局的宽度和高度设置为与父级相匹配。

我在我的膨胀布局中添加了一个空视图(R.layout.new_spending_fragment_layout)并将其参数设置为与xml中的parent匹配,这正在修复问题,但我认为这是一个临时答案。

我还想从各个方面去除填充(或间隙)。

android alertdialog android-alertdialog android-dialogfragment dialogfragment
1个回答
1
投票

试试这个,Dialog with fullscreen screenshot

1 - 创建对话框

Dialog dialog = new Dialog(this, android.R.style.Theme_Light_NoTitleBar); // Replace this line
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_layout);
    dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    dialog.show();

2 - dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="Hello this is demo textview"
            android:textColor="@color/black"
            android:textSize="18sp" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="Fire"
            android:background="@null"/>

    </RelativeLayout>
</RelativeLayout>
© www.soinside.com 2019 - 2024. All rights reserved.