【问题标题】:Android: change the main activity background color using spinner [closed]Android:使用微调器更改主要活动背景颜色[关闭]
【发布时间】:2014-02-25 20:46:07
【问题描述】:

这是 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>

这是字符串.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>

输出应该是当你在微调器上选择一个项目时 Activity 的背景应该改变......

我不知道如何用java编写代码......

【问题讨论】:

    标签: java android xml string android-layout


    【解决方案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 文件夹中没有值 -> strings.xml

    【讨论】:

    • 如果您觉得此答案有帮助,请将其标记为正确答案:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 2013-07-20
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多