【发布时间】:2013-09-23 07:58:40
【问题描述】:
您好,我创建了名为 Group 的简单自定义视图:
public class Group extends LinearLayout {
private TextView headerTextView;
public Group(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.Group, 0, 0);
String string = typedArray.getString(R.styleable.Group_headerText);
typedArray.recycle();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.widget_group, this, true);
headerTextView = (TextView) v.findViewById(R.id.header_text);
headerTextView.setText(string);
}
}
我想在活动中动态创建它。我想设置我的自定义属性属性。 我找到了一些充气的解决方案,但我真的不想使用它;这不是创建对象的正确方法。 我需要一个关于这个领域的例子
Group g = new Group(v.getContext(),arrt);
只是不知道如何设置 arrt 对象并在其中设置我的自定义属性
【问题讨论】:
-
可以添加一个只有Context作为参数的构造函数,然后调用不同视图的setter
标签: android android-custom-view