android - AlertDialog setContentView 不工作 API22

标签 android android-alertdialog

我有一个自定义警报对话框,其中有一个回收 View 和一个自定义适配器。它工作正常。这是我发现在 API22 上没有显示 View 。我就是这个样子

enter image description here

警报是空的。屏幕中间只有一个白色条。

代码如下:

 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>

最佳答案

跟随 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();

关于android - AlertDialog setContentView 不工作 API22,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51487416/

相关文章:

c# - 广播接收器监听以随机间隔触发的连接变化

android - 抽象谷歌客户端 : Fatal signal 11 (SIGSEGV)

android - 使用 Json api 的 Activity 之间的过去信息

Android/SQLite - 跨多列的不同计数

java - 如何创建看起来 TextView 比它大的列表项?

Android 小部件弹出窗口

java - 即使在对话框显示并关闭后,如何保持底部控制栏始终隐藏?

android - 如何在android的非 Activity 类中实现 "finish"之类的东西?

java - 如何更改 Android 中警报对话框的文本行

android - 更改 setmultichoiceitems 中的标准复选框颜色