问题
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);
}
}
回答1:
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>
回答2:
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 ...
来源:https://stackoverflow.com/questions/5816001/androidhow-to-instantiate-my-custom-view-with-attributeset-constructor