1
我试图让我的微调菜单通过更改任何颜色来响应。不能改变任何东西的颜色。选择绿色和之后请先查看图片。我正在学习如何为android编程。无法从微调菜单中获取任何颜色来改变颜色
这里是我的MainActivity.java
package com.romanescotech.mydropdownnavigation;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner;
ArrayAdapter adapter;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
spinner = (Spinner) findViewById(R.id.spinnerMy);
adapter = ArrayAdapter.createFromResource(this, R.array.colors, R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemSelected(AdapterView<?> parent , View view, int pos, long id) {
parent.getItemAtPosition(pos);
String[] colors = getResources().getStringArray(R.array.colors);
String selectedColor = colors[pos];
getWindow().getDecorView().setBackgroundColor(Color.parseColor(selectedColor));
spinner.setBackgroundColor(Color.parseColor(selectedColor));
view.setBackgroundColor(Color.parseColor(selectedColor));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// do nothing
}
}
这里是我的activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinnerMy"
android:alpha=".8" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
这里是我的string.xml
<resources>
<string name="app_name">MyDropDownNavigation</string>
<string-array name="colors">
<item>White</item>
<item>Red</item>
<item>Green</item>
<item>Blue</item>
<item>Yellow</item>
</string-array>
<string name="action_settings">Settings</string>
这里是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这里是我的styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
这里是我的其他V21 /款式XML
<resources>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
这里是它的照片做选择 before making selection
这里之前运行的是做选择后,manues ,背景一切都是相同的颜色。
我猜我的问题就在于此方法。
public void onItemSelected(AdapterView<?> parent , View view, int pos, long id) {
parent.getItemAtPosition(pos);
String[] colors = getResources().getStringArray(R.array.colors);
String selectedColor = colors[pos];
getWindow().getDecorView().setBackgroundColor(Color.parseColor(selectedColor));
spinner.setBackgroundColor(Color.parseColor(selectedColor));
view.setBackgroundColor(Color.parseColor(selectedColor));
在Java代码中
我尝试添加上面的行,它没有做任何事情。在我的方法中添加Log.d行后,我发现onItemSelected方法可能不会被调用,因为Log.d不显示任何内容。怀疑问题在哪里,onItemSelected()方法由于某种原因不会被调用。 – Tarnow