这是ACTIVITY_MAIN.XML
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <Spinner android:layout_margin="150dp" android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/colors_array"/> </LinearLayout>
这是STRING.XML
<string name="app_name">ITMA133QUIZ</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string-array name="colors_array"> <item>Red</item> <item>Orange</item> <item>Yellow</item> <item>Green</item> <item>Blue</item> <item>Violet</item> </string-array>
输出应该是当你在微调器上选择一个项目时,活动的背景应该改变......
我不知道如何在java中编写代码....
答案 0 :(得分:1)
spinner = (Spinner) view.findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.colors_array));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long id) {
switch(position){
case 0: View view = this.getWindow().getDecorView();
view.setBackgroundColor(0xfff00000);
break;
and so on.....
}
}
}
请注意,这假设您的颜色位于 R.array文件夹中没有 - &gt;的strings.xml