簡體   English   中英

DialogFragment 為空,即使提供了 xml

[英]DialogFragment empty, even though xml provided

我正在嘗試使用 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"/>

選擇對話框

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)
}

有什么想法為什么我沒有在對話框中找到我的按鈕嗎?

您已創建View但未將其設置為對話框。 添加

builder.setView(view);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM