如何在 android 布局中使用来自 string.xml 的字符串数组?
How to use String arrays from string.xml in android layouts?
我在我的 strings.xml 文件中创建了一个字符串数组,我想访问数组
我的布局来填补我的筹码。我该怎么做?
这是我的strings.xml
<string-array name="shortcutCategoryNames">
<item>Digital</item>
<item>Clothes</item>
<item>Sport</item>
<item>Cosmetics</item>
</string-array>
这是我的布局
<com.google.android.material.chip.ChipGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.chip.Chip
android:id="@+id/chp_digital"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<com.google.android.material.chip.Chip
android:id="@+id/chp_clothes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<!-- rest of Chips-->
</com.google.android.material.chip.ChipGroup>
您可以使用此代码获得 string-array
:
String strArray[] = getResources().getStringArray(R.array. shortcutCategoryNames);
然后遍历 strArray
以创建 Chip
并以编程方式将这些 Chip
添加到 ChipGroup
您可以使用以下方法获取字符串数组。
ArrayList<String> tagList = new ArrayList();
String[] someArray = getResources().getStringArray(R.array.shortcutCategoryNames);
tagList.addAll(Arrays.asList(someArray));
我在我的 strings.xml 文件中创建了一个字符串数组,我想访问数组 我的布局来填补我的筹码。我该怎么做?
这是我的strings.xml
<string-array name="shortcutCategoryNames">
<item>Digital</item>
<item>Clothes</item>
<item>Sport</item>
<item>Cosmetics</item>
</string-array>
这是我的布局
<com.google.android.material.chip.ChipGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.chip.Chip
android:id="@+id/chp_digital"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<com.google.android.material.chip.Chip
android:id="@+id/chp_clothes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<!-- rest of Chips-->
</com.google.android.material.chip.ChipGroup>
您可以使用此代码获得 string-array
:
String strArray[] = getResources().getStringArray(R.array. shortcutCategoryNames);
然后遍历 strArray
以创建 Chip
并以编程方式将这些 Chip
添加到 ChipGroup
您可以使用以下方法获取字符串数组。
ArrayList<String> tagList = new ArrayList();
String[] someArray = getResources().getStringArray(R.array.shortcutCategoryNames);
tagList.addAll(Arrays.asList(someArray));