如何在工具箱中实现Spinner的Material Design?我尝试了以下方法,但我的Spinner看起来像这样
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/main_color"
app:contentInsetLeft="14dp"
app:contentInsetRight="14dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
<Spinner
android:id="@+id/spinner_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
我的最低SDK版本是15。
在我的onCreate方法中
ButterKnife.inject(this);
mToolbar.inflateMenu(R.menu.menu_login);
mToolbar.setTitle(R.string.sign_in);
mToolbar.setOnMenuItemClickListener(this);
SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.options, android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(spinnerAdapter);
mSpinner.setOnItemSelectedListener(this);
菜单是一张临时纸,始终与应用栏重叠,而不是作为应用栏的延伸。
任何链接都将是很大的帮助。
更新
我已经成功实现了我的目标
我使用了这个
ButterKnife.inject(this);
mToolbar.setTitle(R.string.sign_in);
setSupportActionBar(mToolbar);
SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(getSupportActionBar().getThemedContext(),
R.array.options, android.R.layout.simple_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(spinnerAdapter, this);
和我的 XML
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/app_secondary_color"
app:contentInsetLeft="14dp"
app:contentInsetRight="14dp"
app:popupTheme="@style/ThemeOverlay.AppCompat"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar">
</android.support.v7.widget.Toolbar>
那么,如何使它变为白色,菜单文字的颜色为蓝色呢?另外,我还有一个问题。它似乎太远了?有没有办法修复下拉箭头?
更新2
尝试了很长时间后,我通过创建自定义下拉样式来实现了将其变为白色且文本颜色为黑色。
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:background="@color/background_material_light"
android:textColor="@color/abc_primary_text_material_light"
android:layout_height="48dp"
android:ellipsize="marquee"/>
然后我在我的oncreate中添加了这段代码
spinnerAdapter.setDropDownViewResource(R.layout.custom_simple_spinner_dropdown_item);
现在我遇到的唯一问题是按下状态不起作用,只是一个空白。如果您在另一个弹出菜单中看到,当您单击某个项目时,背景颜色会变得稍微深一些。此外,这个烦人的图标仍然离文本太远。