java - XML 布局未出现在 R.layout 中
问题描述
我正在使用 androidX CardView。为了为卡片制作一个 recyclerview 适配器,我需要为我制作的自定义卡片布局充气。不幸的是,自定义卡片视图的 xml 没有出现在 R.layout.(xml_name) 中,而我通常会找到所有其他布局。
这是我需要膨胀的适配器类中的代码:
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.cardview_item_answer); // not finding this layout
return null;
}
这是卡片布局 xml (cardview_item_answer.xml):
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="160dp"
android:layout_height="160dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="5dp"
app:cardCornerRadius="4dp"
>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_card"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:background="#2d2d2d2d">
</androidx.appcompat.widget.AppCompatImageView>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/text_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test"
android:textSize="13sp"
android:textColor="@color/black"
android:layout_gravity="center"
></androidx.appcompat.widget.AppCompatTextView>
</androidx.appcompat.widget.LinearLayoutCompat>
你有什么想法吗?
非常感谢 !
解决方案
如果 XML 包含任何错误,您的构建可能会失败 - 这将导致 R.layout 产生意外结果。我在 cardview_item_answer.xml 中没有看到 CardView 的结束标记
尝试</androidx.cardview.widget.CardView>
在 cardview_item_answer.xml 的末尾添加一个结束标签
推荐阅读
- if-statement - awk,在健全性检查时跳过当前规则
- c++ - Mac OS X 项目中的 Vorbis 库
- python - 根据总和条件对带有色调的 seaborn 条形图进行排序
- r - 使用 R rvest 抓取表格
- python - 如何根据python中的条件制作词云?
- node.js - NodeJS“如果在 Socket 发出 'data' 事件时没有侦听器,数据将丢失。”
- python - Scrapy Spider 多次抓取相同的东西并丢失其他物品
- javascript - 选项卡结构在 jquery 2.1.3 中工作,在 3.6.0 中显示错误
- python - Python replace() 方法不保留更改
- python - 按多列分组时如何从熊猫数据框中获取N组