AlertDialog setContentView无法正常工作API22

时间:2018-07-23 21:24:27

标签: android alertdialog

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

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>

1 个答案:

答案 0 :(得分:1)

在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();