我有一个来自样本的Android UI,如下所示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:weightSum="1" android:layout_height="wrap_content">
<TextView android:text="data1:"
android:id="@+id/data1Label"
android:layout_weight="0.5"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:text="data2:"
android:id="@+id/data2Label"
android:layout_weight="0.5"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<ListView android:id="@+id/in"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
android:layout_weight="1"
/>
</LinearLayout>
然后我需要在顶部标签面板后面添加一个组合框,然后我添加了一个Spinner组件,如下所示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:weightSum="1" android:layout_height="wrap_content">
<TextView android:text="data1:"
android:id="@+id/data1Label"
android:layout_weight="0.5"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:text="data2:"
android:id="@+id/data2Label"
android:layout_weight="0.5"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<Spinner
android:id="@+id/cmd_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView android:id="@+id/in"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
android:layout_weight="1"
/>
</LinearLayout>
在我的Strings.xml资源文件中,我添加了微调器的数据列表。
<string-array name="cmd_array">
<item>cmd1</item>
<item>cmd2</item>
<item>cmd3</item>
</string-array>
然后在我的活动类的创建方法结束时,我添加了下面的方法来设置数据到微调器
Spinner spinner = (Spinner) findViewById(R.id.cmd_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.cmd_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
但是当我运行我的应用程序时,它并没有向我显示微调器。 有关这个问题的任何想法吗?
我正在将我的应用程序编译为android libaray项目,主要活动是从Unity Game Engine开始。
答案 0 :(得分:0)
以这种方式设置微调器数据:
Spinner spinner = (Spinner) findViewById(R.id.cmd_spinner);
String[] array = getResources().getStringArray(R.array.cmd_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, array);
spinner.setAdapter(adapter);
答案 1 :(得分:0)
你应该试试这个-----
Spinner spinner = (Spinner) findViewById(R.id.cmd_spinner);
String[] stringArray = getResources().getStringArray(R.array.cmd_array);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, stringArray);
spinner.setAdapter(arrayAdapter);
你应该像下面那样修改你的xml,因为这是更好的方法....
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="data1:"
android:id="@+id/data1Label"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<TextView
android:text="data2:"
android:id="@+id/data2Label"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</LinearLayout>
<Spinner
android:id="@+id/cmd_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/in"
android:layout_width="match_parent"
android:layout_height="0dp"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
android:layout_weight="1" />
答案 2 :(得分:0)
您可以查看此answer。您也可以使用自定义适配器,但下面的解决方案适用于简单的情况。
这是重新发布的帖子:
所以如果你来到这里是因为你想在Spinner中同时拥有标签和值 - 这就是我如何做到的:
以通常的方式创建您的Spinner 在array.xml文件中定义2个相等大小的数组 - 一个用于标签,一个用于值数组 使用android:entries =“@ array / labels”
设置您的Spinner当你需要一个值时,做这样的事情(不,你不必链接它):
String selectedVal = getResources().getStringArray(R.array.values)[spinner.getSelectedItemPosition()];