android - 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>
解决方案
在 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();
推荐阅读
- kubernetes - GCP 上的 Kubernetes 集群缺少编辑权限
- sql - SQL - 将时间序列事件转换为开/关对(处理可能丢失的开或关)
- python - Python请求模块不遵循重定向文件下载
- javascript - javascript中类内的引用类型未按预期工作
- javascript - 如何用 js 定义数学值,然后将这些值相乘?
- iis - VS 2017 - Web 应用程序项目 [MyProject] 配置为使用 IIS。您无权访问 IIS 配置文件
- jquery - 带有 django 的 JQuery AJAX 获取 csrf 错误 403
- r - Rmarkdown 中一组特定行的 Calibri 字体
- python - 如何在python3中将“\”(反斜杠)作为字符串而不是作为“转义”字符?
- java - 如何在 Java 字符串中替换一种以上的字符