0
我有一个LinearLayout
与其中一些Views
。然后我想把这个元素当作View
,所以我创建了一个新的class
,从LinearLayout
延伸出来。现在,当我动态地将此class
的新instance
添加到布局中时,我什么都看不到。我相信我必须从class
得到View
,但不知道如何。有没有可能以某种方式将这个新类与xml相关联?如何处理一些xml结构作为视图?
更新:
public class Task extends LinearLayout {
public Task(Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.task_view, this, false);
}
public Task(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.task_view, this, false);
}
public Task(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(R.layout.task_view, this, false);
}
}
然后:
Task newTask = new Task(getActivity());
someLinearLayout.addView((View) newTask); // happens nothing
仅仅在构造函数中使它膨胀就足够了吗?或者我需要将它返回到某个地方? – Eugene
这就够了,别忘了重写其他2个构造函数。 –
到目前为止还没有工作,我在代码中加入了一些代码,请检查。 – Eugene