【发布时间】:2012-10-03 07:46:58
【问题描述】:
我正在创建自己的自定义 LinearLayout,并扩展它的类,我需要使用 addview 方法将子级添加到布局中,
我需要访问在构造函数之后设置的标签,所以我需要知道执行此类操作的最佳实践
这是我的代码:
public class MyLayout extends LinearLayout {
public MyLayout(Context context) {
super(context);
MyTag tag = (MyTag) getTag();
// Parent layout
setOrientation(VERTICAL);
setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
setBackgroundColor(tag.getBgColor());
}
}
【问题讨论】:
-
getTag() 将始终在构造函数中返回 null。 (你不能设置它,直到构造对象)
-
好吧,伙计们,我知道这个问题,需要另一种在布局显示上调用一次的方式或方法......我检查了 onLayout 方法,认为它被调用了多次并导致代码运行多次。 ..需要更专业的方式
标签: android android-linearlayout addchild