【发布时间】:2016-02-02 17:17:12
【问题描述】:
我正在努力实现两件事:
- 让我的 Spinner 继承 AppCompat 主题。
- 为微调器元素添加图标,就像在工具栏弹出菜单中一样。
由于我无法实现第一点,所以我专注于这一点,但我也想稍后添加图标。现在,我的工具栏弹出菜单继承了 AppCompat 主题,但 Spinner 没有,如下图所示。第一个图像显示了来自工具栏的(正确的)弹出菜单,而第二个显示了来自 Spinner 的弹出菜单。这是不继承样式的 Spinner 示例。还是应该期待这种弹出菜单样式?
我已经尝试了很多东西,所以这个问题可能有多个重复项,但我无法让它发挥作用。下面的代码有什么问题?正确继承主题后,以后如何添加图标?最低 SDK 版本为 16,目标为 23。
themes.xml:
<resources>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">@color/my_brown</item>
<item name="colorPrimaryDark">@color/my_dark_gray</item>
<item name="colorAccent">@color/my_green</item>
<!-- This is just a test, it makes no difference. -->
<item name="android:spinnerStyle">@style/MySpinnerStyle</item>
</style>
<style name="MyTheme" parent="MyTheme.Base"></style>
<!--
ActionBar style, applied directly to XML elements
-->
<style name="MyActionBarStyle" parent="@style/Widget.AppCompat.ActionBar">
<item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@color/my_brown</item>
<item name="android:minHeight">?attr/actionBarSize</item>
</style>
<!--
Spinner style, for testing. Also tried applied directly to xml Spinners.
-->
<style name="MySpinnerStyle" parent="@style/Widget.AppCompat.Spinner">
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="android:popupMenuStyle">@style/Widget.AppCompat.Light.PopupMenu</item>
</style>
</resources>
微调器的设置很简单:
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, mCategories);
mSpinner.setAdapter(adapter);
其中mSpinner 是膨胀的 Spinner,mCategories 是一个字符串数组。在 XML 中 Spinner 被定义为
<Spinner
android:id="@+id/my_spinner"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
我试过直接给 Spinner 添加各种样式,但是不行。
在我的 AndroidManifest.xml 中,我在 Application 标签中添加了以下内容:
android:theme="@style/MyTheme"
【问题讨论】:
标签: android material-design android-spinner android-appcompat android-theme