Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
921 views
in Technique[技术] by (71.8m points)

xml - Is it possible to access a string-array item in Android?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...