首页 > 解决方案 > AlertDialog setContentView 不工作 API22

问题描述

我有一个自定义警报对话框,其中包含一个 recyclerview 和一个自定义适配器。它工作正常。我发现在 API22 上没有显示视图。我只是看起来像这样

在此处输入图像描述

警报为空。屏幕中间只有一个白条。

这是代码:

 String itemSelected = foodList.get(position).getName();

    ArrayList<SearchItem> searchItemArrayList =  ListOfItemsClass.getArrayListAboutCategory(itemSelected, this);

    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.more_info_dialog_layout);
    dialog.setCanceledOnTouchOutside(true);
    dialog.setCancelable(true);
    dialog.show();

    RecyclerView moreInfoReyclcerView = (RecyclerView) dialog.findViewById(R.id.more_info_recycler_view);
    moreInfoReyclcerView.setHasFixedSize(true);
    moreInfoReyclcerView.setLayoutManager(new LinearLayoutManager(this));
    MoreInfoAdapter moreInfoAdapter = new MoreInfoAdapter(searchItemArrayList);
    moreInfoReyclcerView.setAdapter(moreInfoAdapter);

这是xml

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

<android.support.v7.widget.RecyclerView
    android:padding="5dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/more_info_recycler_view"> . 
</android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

标签: androidandroid-alertdialog

解决方案


在 Peterstev Uremgba 之后,使用 AlertDialog.Builder 类解决了这个问题:

 String itemSelected = notEatenThisWeekItemsYet.get(position).getName();

    ArrayList<SearchItem> searchItemArrayList =  ListOfItemsClass.getArrayListAboutCategory(itemSelected, this);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            this);

    LayoutInflater myLayout = LayoutInflater.from(this);
    final View dialogView = myLayout.inflate(R.layout.more_info_dialog_layout, null);

    alertDialogBuilder.setView(dialogView);



    alertDialogBuilder.setCancelable(true);

    RecyclerView moreInfoReyclcerView = (RecyclerView) dialogView.findViewById(R.id.more_info_recycler_view);
    moreInfoReyclcerView.setHasFixedSize(true);
    moreInfoReyclcerView.setLayoutManager(new LinearLayoutManager(this));
    MoreInfoAdapter moreInfoAdapter = new MoreInfoAdapter(searchItemArrayList);
    moreInfoReyclcerView.setAdapter(moreInfoAdapter);

    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.setCanceledOnTouchOutside(true);

    alertDialog.show();

推荐阅读