android:如何使用属性集构造函数实例化我的自定义视图

发布于 2024-11-03 19:24:01 字数 1546 浏览 5 评论 0原文

我的自定义视图具有动态自定义属性,例如背景图像属性,通过本周分配。 我不想使用构造函数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妳是的陽光 2024-11-10 19:24:01

您必须在位于值中的 attr.xml 文件上将属性声明为可

样式:

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

之后,您必须在自定义视图中将它们用作 this ,并且必须声明两个默认构造函数:

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

这应该可以工作,以获取参数您的自定义视图。如果您不明白,请随时询问。

typeFaceindex 只是一个有效的示例。

之后,您必须像其他任何一样将您的 customView 使用到布局中:

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

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>
梦里的微风 2024-11-10 19:24:01

也许我不明白你在做什么或为什么这样做,但我发现你对 AttributeSet 的尝试非常复杂。

我建议您创建另一个像这样的构造函数

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

    setBackgroundResource(backgroundRessourceID);
}

然后像这样实例化您的 CalendarView

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

希望这可以解决您的问题...

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 ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文