即使提供了xml,DialogFragment也为空

时间:2019-04-15 19:43:36

标签: java android kotlin android-dialogfragment

我正在尝试创建一个带有DialogFragment的弹出窗口,但是我的对话框仅包含标题,否定和肯定按钮。

Layout_selection_dialog

<Button
        android:text="Aalborg"
        android:layout_width="200dp"
        android:layout_height="wrap_content" android:id="@+id/firstCityBtn"
        app:layout_constraintStart_toStartOf="parent"/>
<Button
        android:text="Aarhus"
        android:layout_width="200dp"
        android:layout_height="wrap_content" android:id="@+id/thirdCityBtn"
        app:layout_constraintBottom_toBottomOf="parent"/>
<Button
        android:text="København"
        android:layout_width="200dp"
        android:layout_height="wrap_content" android:id="@+id/secondCityBtn"
        android:layout_marginTop="8dp"/>

SelectionDialog

public class SelectionDialog extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.layout_selection_dialog, null);

        builder.setTitle("Vælg by").setNegativeButton("Back", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).setPositiveButton("Save", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });

        return builder.create();
    }
}

要在MainActivity中调用的函数

fun showCitySelection() {
   var dialog: CitySelectionDialog = CitySelectionDialog()
   var ft: FragmentTransaction = supportFragmentManager.beginTransaction()
    dialog.show(ft, null)
}

有什么想法为什么我没有在对话框中显示按钮?

1 个答案:

答案 0 :(得分:0)

您已创建View,但未将其设置为对话。添加

builder.setView(view);