如何在android的自定义组件中处理@string属性?

时间:2012-06-14 03:08:47

标签: android custom-component

在布局xml文件中,我使用@string设置属性,如下所示:

<com.hardpass.MyCustomComponent
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    mynamespace:title="@string/xxootitle" />

如何处理自定义组件@string/xxootitle中的com.hardpass.MyCustomComponent? 或者如何将@string/xxootitle解析为在strings.xml中定义的实际值。

String title = attrs.getAttributeValue(mynamespace, "title"); 
//the title got here is "@string/xxootitle",
//but how to parse `@string/xxootitle` to the real value which is defined in strings.xml.

1 个答案:

答案 0 :(得分:2)

使用getAttributeResourceValue和getString,

getString(attrs.getAttributeResourceValue(mynamespace, "title", R.string.xxootitle));