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

Posted

技术标签:

【中文标题】android:如何使用属性集构造函数实例化我的自定义视图【英文标题】:android:how to instantiate my custom view with attributeset constructor 【发布时间】:2011-08-14 12:46:53 【问题描述】:

我的自定义视图具有动态自定义属性,例如backgroundimage 属性,通过当前周分配。 我不想使用构造函数 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);


【问题讨论】:

好奇你是怎么做到的?我有同样的问题想要动态添加我的自定义视图,但不确定如何获取和设置通用属性和 3 个自定义属性 【参考方案1】:

您必须在 attr.xml 文件中将属性声明为可设置样式

例如:

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

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

  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>

【讨论】:

【参考方案2】:

也许我不明白您在做什么或为什么要这样做,但我发现您对 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));

希望这能解决您的问题...

【讨论】:

谢谢,我现在是这样用的,但是有很多属性要使用,包括自定义和系统提供,我很好奇如何使用属性集。

以上是关于android:如何使用属性集构造函数实例化我的自定义视图的主要内容,如果未能解决你的问题,请参考以下文章

Custom AppComponentFactory无法实例化应用程序

我无法使用自定义讲师实例化我的课程有啥问题? [关闭]

构造函数和实例的关系

如何使用 Kotlin 在 Spring Test 类中注入服务组件?

自动属性无法在 C# 实例构造函数中初始化

Android 使用Kotlin来实现水波纹的自定义View