使用AppCompat实现的Material Design反色Spinner/EditText主题

3
我在创建一个使用AppCompat v21主题的Material Design应用时遇到了问题。首先,我使用了带有暗色操作栏的AppCompat light应用程序主题。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Main theme colors -->
    <!--   your app branding color for the app bar -->
    <item name="colorPrimary">@color/theme_color</item>
    <!--   darker variant for the status bar and contextual app bars -->
    <item name="colorPrimaryDark">@color/theme_color_dark</item>
    <!--   theme UI controls like checkboxes and text fields -->
    <item name="colorAccent">@color/theme_color_accent</item>
</style>

我的应用程序所使用的配色方案很暗,因此我使用了DarkActionBar变体。我的问题是,我有一些布局需要在其中放置Spinner和EditText,而这些布局的背景颜色是我的theme_color,也就是黑色。这导致Spinner/EditText在暗色背景上显示为黑色。我该怎么做才能使这些控件的样式/主题使用Dark变体,以便它们显示为白色文本而不是黑色文本。更糟糕的是,我希望Spinner显示白色文本,但Spinner的下拉选项显示浅色版本。请问有什么方法可以实现吗?

AutoCompleteTextView尚未进行样式设置 - 请参阅此处获取更多详细信息:https://code.google.com/p/android/issues/detail?id=77742 - ahmedre
3个回答

0
所以,您正在寻找一种方法将不同的主题应用于选择视图。 对于大多数元素(除了您已经知道的Toolbar),没有纯xml的方法来实现这一点。
您将不得不手动膨胀视图,使用ContextThemeWrapper
ContextThemeWrapper wrapper = new ContextThemeWrapper(getActivity(),
    R.style.Theme_AppCompat); // The dark one
LayoutInflater footPump = LayoutInflater.from(getActivity()).cloneInContext(wrapper);
// Note null as second argument -- if you pass in parent view, theme will
// be taken from the parent
Spinner spinner = (Spinner) footPump.inflate(R.layout.spinner, null);

要为填充器创建布局资源,只需创建一个单视图的 XML 文件:

<!-- res/layout/spinner.xml -->
<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

0
这样怎么样?
为您的小部件制作一个样式,例如:
<style name="CustomEditText" parent="android:Widget.EditText">
      <item name="android:textColor">@color/red</item>
</style>

然后在你的xml文件中,给你的EditText添加这个属性

style="@style/CustomEditText"

我可以这样做,但很可能会导致一些“科学怪人”控件。我的意思是最好使用AppCompat的暗色控件。因为在这种情况下,所有东西都将被正确地皮肤化。我只是不知道如何仅针对几个控件使用AppCompat暗色。 - Steve Garon

0

我在带有深色背景的工具栏中使用了一个Spinner,并记得Google I/O Android应用程序也是这样做的,因此我查看了其样式 - 更具体地说,是ActionBarThemeOverlay here

在我使用以下属性后,我的主题上的Spinner背景颜色(“箭头”)发生了变化:

<item name="colorControlNormal">#fff</item>
<item name="colorControlHighlight">#3fff</item>

colorControlHighlight 是用户按下 Spinner 时的颜色。不过我没有在 EditText 上测试过。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接