我正在实现一个自定义视图,并希望在attrs.xml中定义一个字符串数组。目前,我所做的工作如下:
在attrs.xml文件中,我定义了:
<attr name="twCells" format="string"/>在activity_main.xml中,我提供了以下数据:
app:twCells="A1,A7,G1,G7"最后,在MyCustomView.java文件中,我读取twCell属性如下:
private List<String> twCells = null;
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BoardView, 0, 0);
twCells = Arrays.asList(a.getString(R.styleable.BoardView_twCells).split(","));我的问题是:有没有更好的方法可以做到这一点?
发布于 2014-03-19 20:53:29
如果您同意将字符串资源移动到strings.xml文件中,您可以这样做:
in strings.xml
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>为了得到一根绳子:
String planet = getResources().getStringArray(R.array.planets_array)[i];https://stackoverflow.com/questions/22514979
复制相似问题