【问题标题】:Passing XML parameters to custom views将 XML 参数传递给自定义视图
【发布时间】:2011-01-11 19:29:18
【问题描述】:

我正在尝试实现一个自定义视图,我希望能够通过 XML 布局文件为其传递参数。问题是这些参数或多或少是任意的;它们最终将被用作构造 URL 的参数(构建和维护有效参数的完整列表是不值得的)。

因此,ApiDemos 示例代码中的方法不一定合适,因为我不知道参数的名称。

我尝试做的事情如下:

public Chart(Context context, AttributeSet attributes) {
    super(context, attributes);
    params = new HashMap<String, String>();

    for(int i = 0; i < attributes.getAttributeCount(); i++) 
        params.put(attributes.getAttributeName(i), attributes.getAttributeValue(i));
}

这可能不是解决这个问题的最佳方法.. 但我遇到的主要问题是如何过滤掉 android: 属性(即我不希望 android:layout_height 等出现在参数中) ?

【问题讨论】:

    标签: android android-widget android-layout


    【解决方案1】:

    通过使用 getAttributeValue() 的重载版本找到了一种方法。如果有更好的方法,仍然开放供输入。

    【讨论】:

    • 其实我可能需要一个更好的解决方案。如果我这样做,我似乎无法使用样式..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 2011-06-27
    • 2011-11-10
    • 1970-01-01
    相关资源
    最近更新 更多