android - 如何在 android 布局中使用 string.xml 中的字符串数组?

标签 android arrays android-layout android-resources

我在 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>

最佳答案

您可以使用以下方法获取字符串数组。

ArrayList<String> tagList = new ArrayList();

String[] someArray = getResources().getStringArray(R.array.shortcutCategoryNames);
tagList.addAll(Arrays.asList(someArray));

关于android - 如何在 android 布局中使用 string.xml 中的字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55355183/

相关文章:

Android - android.os.NetworkOnMainThreadException

java - 以逗号分割字符串

java - 使用 system.out.println 打印包含 int 值的数组

android - 按钮不引起 onClick 事件

Android 库、Kotlin 和 Dagger2

java - 在我的代码中找不到 java.io.OptionalDataException 的原因

android - 如何操作APK变体版本代码

php - 从关联数组中提取值时遇到问题

android - 线性布局中的按钮重量

android - 将新项目计数添加到按钮上的图标 - Android