繁体   English   中英

我的布局充气器向我的线性布局添加自定义视图不起作用

[英]My layout inflater to add a custom view to my Linear Layout isnt working

所以我正在尝试使用布局充气器在线性布局内创建一个自定义视图。 我以前从未这样做过,所以如果我犯了一个愚蠢的错误,请原谅我。 这是针对我正在开发的任务应用程序...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM