android:如何使用属性集构造函数实例化我的自定义视图
我的自定义视图具有动态自定义属性,例如背景图像属性,通过本周分配。 我不想使用构造函数 CalendarView(Context context, AttributeSet attrs) 来传递多个属性,并且我尝试使用 Xml.asAttributeSet 实例化属性集,但它无法工作。谁能告诉我该怎么做。
注意:我的自定义视图具有动态属性,因此我不想通过 xml 布局实例化自定义视图。我的解决方案不正确?
这是自定义视图:
public class CalendarView extends View {
int backgroundImage;
public CalendarView(Context context, AttributeSet attrs) {
super(context, attrs);
backgroundImage = attrs.getAttributeResourceValue("http://www.mynamespace.com", "backgroundimage", 0);
}
}
这是活动:
public class TestActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(createTestView(0));
}
public CalendarView createTestView(int currentWeek) throws XmlPullParserException, IOException {
String attributes = "<attribute xmlns:android=\"http://schemas.android.com/apk/res/android\" " +
"xmlns:test=\"http://www.mynamespace.com\" " +
"android:layout_width=\"fill_parent\" android:layout_height=\"30\" " +
"test:backgroundimage=\"@drawable/"+ currentWeek +"_bg" + "\"/>";
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(new StringReader(attributes));
parser.next();
AttributeSet attrs = Xml.asAttributeSet(parser);
return new CalendarView(this,attrs);
}
}
My custom view has dynamic custom attribute,e.g. the backgroundimage attribute,assign via current week.
I wan't to use construtor CalendarView(Context context, AttributeSet attrs) to pass several attribute,and I try to instantiate the attributeset with Xml.asAttributeSet,but it can't work. can anyone tell me how to do.
Note:my custom view has dynamic attribute ,so I don't wan't to instantiate the custom view via xml layout. my solution is incorrect ?
here is the custom view:
public class CalendarView extends View {
int backgroundImage;
public CalendarView(Context context, AttributeSet attrs) {
super(context, attrs);
backgroundImage = attrs.getAttributeResourceValue("http://www.mynamespace.com", "backgroundimage", 0);
}
}
here is the activity:
public class TestActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(createTestView(0));
}
public CalendarView createTestView(int currentWeek) throws XmlPullParserException, IOException {
String attributes = "<attribute xmlns:android=\"http://schemas.android.com/apk/res/android\" " +
"xmlns:test=\"http://www.mynamespace.com\" " +
"android:layout_width=\"fill_parent\" android:layout_height=\"30\" " +
"test:backgroundimage=\"@drawable/"+ currentWeek +"_bg" + "\"/>";
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(new StringReader(attributes));
parser.next();
AttributeSet attrs = Xml.asAttributeSet(parser);
return new CalendarView(this,attrs);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在位于值中的 attr.xml 文件上将属性声明为可
样式:
之后,您必须在自定义视图中将它们用作 this ,并且必须声明两个默认构造函数:
这应该可以工作,以获取参数您的自定义视图。如果您不明白,请随时询问。
typeFaceindex 只是一个有效的示例。
之后,您必须像其他任何一样将您的 customView 使用到布局中:
You have to declare the attribute as styleable on attr.xml file located in values the
for example:
After that you have to use them as this in your custom view, and you must declare both default constructors:
This should work, to get parameters for your custom view. Feel free to ask if you don't undersand.
the typeFaceindex is just an example which works.
After that you have to use your customView into layout like any other this:
也许我不明白你在做什么或为什么这样做,但我发现你对 AttributeSet 的尝试非常复杂。
我建议您创建另一个像这样的构造函数
然后像这样实例化您的 CalendarView
希望这可以解决您的问题...
Maybe I don't get what you are doing or why you're doing it, but I find your attempt with the AttributeSet quite complicated.
I'd suggest you create another constructor like this
And then instantiate your CalendarView like this
Hopefully this solved your issue ...