Is it possible to access a string-array item in An

2020-02-13 07:53发布

问题:

snip-code from values/string.xml:

<array name="categories">
    <item name="today">Today</item>
    <item name="life">Life</item>
    <item name="corner">Corner</item>
    <item name="banks">Banks</item>
    <item name="it">IT</item>
    <item name="fun">Fun</item>
</array>

snip-code from layout/main.xml:

<Button
    android:id="@+id/today"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="8sp"
    android:text="@array/categories"
    />

Is it possible to assign a label-name to a Button with id=today directly from values/string.xml using string-array with name=categories and specific item, in this case - today?

回答1:

See the selected answer here: Android - reference a string in a string array resource with xml.

According to that answer, have to do something like this:

<string name="earth">Earth</string>
<string name="moon">Moon</string>

<string-array name="system">
    <item>@string/earth</item>
    <item>@string/moon</item>
</string-array>

Then you would simply do:

  <Button ....
  android:text="@string/earth" />