机器人:如何实例化我的自定义视图属性集中的构造 [英] android:how to instantiate my custom view with attributeset constructor

查看:194
本文介绍了机器人:如何实例化我的自定义视图属性集中的构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义视图具有动态的自定义属性,如:在和backgroundImage属性,通过电流周分配。 我wan't使用construtor CalendarView(上下文的背景下,ATTRS的AttributeSet)通过几个属性,我尝试实例化Xml.asAttributeSet AttributeSet中,但它不能正常工作。谁能告诉我该怎么做。

注意:?我的自定义视图具有动态属性,所以我不wan't实例通过XML布局中的自定义视图我的解决方法是不正确

下面是自定义视图:

 公共类CalendarView扩展视图{
    INT和backgroundImage;
    公共CalendarView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        将backgroundImage = attrs.getAttributeResourceValue(http://www.mynamespace.com,将backgroundImage,0);
    }
}
 

下面是活动:

 公共类TestActivity延伸活动{

    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(createTestView(0));
    }


公共CalendarView createTestView(INT currentWeek)抛出XmlPullParserException,IOException异常{
    字符串属性=<属性的xmlns:机器人= \HTTP://schemas.android.com/apk/res/android \+
        的xmlns:测试= \HTTP://www.mynamespace.com \+
        机器人:layout_width = \FILL_PARENT \机器人:layout_height = \30 \+
        测试:将backgroundImage = \@绘制/+ currentWeek +_ BG+\/>中;

    XmlPullParserFactory工厂= XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(真正的);
    XmlPullParser解析器= factory.newPullParser();
    parser.setInput(新StringReader(属性));
    parser.next();
    AttributeSet中的attrs = Xml.asAttributeSet(分析器);
    返回新CalendarView(这一点,ATTRS);
}
}
 

解决方案

您必须声明属性为设置样式在位于值attr.xml文件中的

例如:

 <申报,设置样式名称=yourView>
    < attr指示NAME =aAttr格式=维/>
    < attr指示NAME =bAttr格式=维/>
   >
 

之后,你必须使用它们,因为这在您的自定义视图,您必须声明包括默认的构造函数:

 公共CalendarView(上下文的背景下){
     超(上下文);
   }

公共CalendarView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    和backgroundImage = attrs.getAttributeResourceValue(http://www.mynamespace.com
   将backgroundImage,0);
    INT typefaceIndex = attrs.getAttributeIntValue(http://schemas.android.com/apk/res/android,字体,0);
    TypedArray A = context.obtainStyledAttributes(ATTRS,R.styleable.yourView);
}
 

这应该工作,让您的自定义视图参数。随意问如果你不undersand。

在typeFaceindex只是其工作的一个例子。

之后,你必须使用你的customView到布局就像任何其他的:

 < com.example.yourView> < /com.example.yourView>
 

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

解决方案

You have to declare the attribute as styleable on attr.xml file located in values the

for example:

<declare-styleable name="yourView">
    <attr name="aAttr" format="dimension" />
    <attr name="bAttr" format="dimension" />
   >

After that you have to use them as this in your custom view, and you must declare both default constructors:

  public CalendarView(Context context) {
     super(context);
   }

public CalendarView(Context context, AttributeSet attrs) {
    super(context, attrs);
    backgroundImage = attrs.getAttributeResourceValue("http://www.mynamespace.com", 
   "backgroundimage", 0); 
    int typefaceIndex = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "typeface", 0);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.yourView);
}

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:

<com.example.yourView> </com.example.yourView>

这篇关于机器人:如何实例化我的自定义视图属性集中的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆