java - 我的布局充气器向我的线性布局添加自定义视图不起作用
问题描述
所以我正在尝试使用布局充气器在线性布局内创建一个自定义视图。我以前从未这样做过,所以如果我犯了一个愚蠢的错误,请原谅我。这是针对我正在开发的任务应用程序...
String subtaskName = getIntent().getStringExtra("subtaskName");
boolean priHigh = getIntent().getBooleanExtra("priHigh", false);
boolean priMed = getIntent().getBooleanExtra("priMed", false);
boolean priLow = getIntent().getBooleanExtra("priLow", false);
boolean timeMore = getIntent().getBooleanExtra("timeMore", false);
boolean timeMed = getIntent().getBooleanExtra("timeMed", false);
boolean timeLess = getIntent().getBooleanExtra("timeLess", false);
for (int i = 0; i <=15; i++)
{
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.subtask_item, subtaskLinLay, false);
TextView subName = rowView.findViewById(R.id.tvSubtaskName);
ImageView ivPri = rowView.findViewById(R.id.ivPri);
ImageView ivTime = rowView.findViewById(R.id.ivTime);
rowView.setId(i);
subName.setText(subtaskName);
if (priHigh)
{
ivPri.setImageResource(R.drawable.priority_high);
}
if (priMed)
{
ivPri.setImageResource(R.drawable.priority_med);
}
if (priLow)
{
ivPri.setImageResource(R.drawable.priority_low);
}
if (timeMore)
{
ivTime.setImageResource(R.drawable.time_symbol_more);
}
if (timeMed)
{
ivTime.setImageResource(R.drawable.time_symbol_med);
}
if (timeLess)
{
ivTime.setImageResource(R.drawable.time_symbol_less);
}
}
解决方案
请注意,您应该更喜欢使用RecyclerView
来显示列表。
此代码的解决方案:
// "this" is Context here
LayoutInflater inflater = LayoutInflater.from(this);
// the last arg should be true (attach to parent)
View rowView = inflater.inflate(R.layout.subtask_item, subtaskLinLay, true);
推荐阅读
- java - 本地通知未在所有设备中触发
- wordpress - 无法在 woocommerce 我的帐户页面中编辑标记
- wordpress - 基于 sku 的自定义货币 - WooCommerce
- docker - 现在正在监听哪个端口?暴露与端口?
- java - 什么时候可以在 android 中使用强引用并且此代码是否泄漏?
- python - 我正在尝试使用 selenium webdriver 从 instagram 中抓取名称?
- mysql - 仅对 MySQL 结果中的 id 重复列
- c# - UWP MediaPlayerElement - 如何循环/重复 mp4
- javascript - 引导轮播下一个和上一个图像不显示
- php - Stocktwits API - 如何自动登录