开发者

android:how to instantiate my custom view with attributeset constructor

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.myname开发者_运维百科space.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>


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

public CalendarView(Context context, int backgroundRessourceID) {
    super(context);

    setBackgroundResource(backgroundRessourceID);
}

And then instantiate your CalendarView like this

CalendarView cv = new CalendarView(this, R.drawable.0_bg);
cv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 30));

Hopefully this solved your issue ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜