Android Html.fromHtml()文本格式和多维数组
我正在开发的应用程序文本量很大,有几个可扩展的列表,所以我使用多维数组来填充子视图。 我想加粗文本的某些部分,如下所示: 例1:这是一个例子。 为此,我在strings.xml中有以下内容:Android Html.fromHtml()文本格式和多维数组,android,arrays,string,Android,Arrays,String,我正在开发的应用程序文本量很大,有几个可扩展的列表,所以我使用多维数组来填充子视图。 我想加粗文本的某些部分,如下所示: 例1:这是一个例子。 为此,我在strings.xml中有以下内容: <string name="example"><![CDATA[<b>Example 1:</b> This is an example.]]></string> 想象成吨,然后我把它们放进我的阵列中: private CharSequence
<string name="example"><![CDATA[<b>Example 1:</b> This is an example.]]></string>
想象成吨,然后我把它们放进我的阵列中:
private CharSequence [][] childview = {
{example, example1, example2, example3};
{example4, example5, example6, example7};
};
问题是,必须有一个更好的方法来实现这一点,而不是创建一大堆变量,对吗?我可以将我的字符串制作成字符串数组,并使用Html.fromHtml格式将它们放入多维数组吗?您可以编写一个小实用方法:
public CharSequence [][] fromRes(int[][] resIdMatrix) {
CharSequence[][] result = new CharSequence[resIdMatrix.length][];
int i = 0;
for (int[] resIds : resIdMatrix) {
CharSequence[] row = result[i++] = new CharSequence[resIds.length];
int j = 0;
for (int id : resIds) {
row[j++] = Html.fromHtml(getString(id));
}
}
return result;
}
然后用二维资源ID数组调用它:
int[][] resIds = {
{R.string.example, R.string.example1, R.string.example2, R.string.example3},
{R.string.example4, R.string.example5, R.string.example6, R.string.example7},
};
CharSequence[][] childview = fromRes(resIds);
那是一个比一个更大的数字
int[][] resIds = {
{R.string.example, R.string.example1, R.string.example2, R.string.example3},
{R.string.example4, R.string.example5, R.string.example6, R.string.example7},
};
CharSequence[][] childview = fromRes(resIds);