使用字符串数组填充Spinner [英] Use string array to populate Spinner

查看:72
本文介绍了使用字符串数组填充Spinner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android应用程序方面,我属于菜鸟联盟,我希望使用下面的数组或字符串(这是一个转换器应用程序)填充Spinner,这是我的XML文件的摘录,并且我希望填充微调框:

I'm in the rookie leagues when it comes to Android apps and am looking to populate a Spinner with an Array or strings (it's a converter app) below is an extract from my XML file and I'm looking to populate the the Spinner:

......
<string name="TemperatureString">Temperature</string>
<string name="WeightString">Weight</string>
<string name="VolumeString">Volume</string>
<string name="SpeedString">Speed</string>
<string name="LengthString">Length</string>
<string name="AreaString">Area</string>
<string name="EnergyString">Energy</string>
<string name="PresureString">Presure</string>
<string name="MemoryString">Memory</string>

<string-array name="Convert_Type">
    <item>@string/TemperatureString</item>
    <item>@string/WeightString</item>
    <item>@string/VolumeString</item>
    <item>@string/SpeedString</item>
    <item>@string/LengthString</item>
    <item>@string/AreaString</item>
    <item>@string/EnergyString</item>
    <item>@string/PresureString</item>
    <item>@string/MemoryString</item>
</string-array>

据此,我正在尝试填充微调器(@+id/MainSpinner)-我不确定我在这里做什么,但是对于activity_main.xml我有以下内容:

From this, I'm trying to populate my spinner (@+id/MainSpinner) - I'm not sure what I'm doing here but for the activity_main.xml I have the following:

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/MainSpinner"
        tools:listitem="@layout/support_simple_spinner_dropdown_item"/>

我知道有一种通过Java做到这一点的方法,但我在Java方面更糟! 因此,如果可能,我希望将其保留在xml中.

I know there's a way of doing this via Java but I'm even worse at Java! For this reason, I'd like to keep this within xml if possible.

此外,如果有人可以向我指向一些链接,这些链接将带给我很棒的Java和xml技能-我从Udacity开始,发现它们很好,但是对于非IT人士来说,还有很多事情需要接受研究生(我从事金融工作,但发现这种事情真的很有趣!)

Also, If someone can want's to point me towards links to bring on my Java and xml skills that would be great - I've started with Udacity and have found them good but there's a lot to take in for a non-IT graduate (I work in finance but find this kinda thing really interesting!)

提前谢谢!

推荐答案

将ListView和Spinner控件与字符串数组绑定的最简单方法是

Simplest way to bind ListView and Spinner control with String Array is

android:entries = "@array/nameofarray"

<Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/MainSpinner"
        tools:listitem="@layout/support_simple_spinner_dropdown_item"
        android:entries="@array/Convert_Type"/>

如果要更改微调器的每个项目的主题,则将下面的样式放入res/values/styles.xml

If you want to change the theme of each item of Spinner then put below style into res/values/styles.xml

<style name="ItemTextAppearance">
    <item name="android:textColor">#f00</item>
    <item name="android:textStyle">bold</item>
    <item name="android:typeface">monospace</item>
</style>

和集合

android:theme="@style/ItemTextAppearance"

微调器.

这篇关于使用字符串数组填充Spinner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆