Android 使用AppCompat的材质设计反向微调器/编辑文本主题

Android 使用AppCompat的材质设计反向微调器/编辑文本主题,android,android-edittext,spinner,android-appcompat,material-design,Android,Android Edittext,Spinner,Android Appcompat,Material Design,我在创建具有AppCompat v21主题的材质设计应用程序时遇到问题。首先,我使用AppCompat light with dark actionbar创建了我的应用程序主题 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Main theme colors --> <!-- your app branding color for the app

我在创建具有AppCompat v21主题的材质设计应用程序时遇到问题。首先,我使用AppCompat light with dark actionbar创建了我的应用程序主题

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

@颜色/主题颜色
@颜色/主题\u颜色\u深色
@颜色/主题\u颜色\u重音
我的应用程序的颜色品牌非常暗,这就是我使用DarkActionBar变体的原因。我的问题是,我有两个布局,我想在布局中放置一个
微调器
和一个
编辑文本
,其中背景是我的
主题颜色
,这是深色的。这将导致在深色背景上使用黑色的
微调器/EditText
。如何使这些控件的样式/主题使用黑色变体,使它们以白色文本而不是黑色文本显示。更糟糕的是,我需要微调器显示白色文本,但需要微调器的下拉项来显示轻型版本。

这个怎么样

为您的小部件制作样式,例如:

<style name="CustomEditText" parent="android:Widget.EditText">
      <item name="android:textColor">@color/red</item>
</style>

我在一个黑色背景的工具栏中使用微调器,我记得谷歌I/O Android应用程序也是这样做的,所以我看了一下它的风格——更具体地说,是ActionBarThemeOverlay

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

#fff
#3fff

colorControlHighlight是用户按下微调器时的颜色。不过,我没有在编辑文本上进行测试

因此,您正在寻找一种方法来应用不同的主题来选择视图。 对于大多数元素,没有唯一的xml方法可以做到这一点(除了
工具栏
,您已经知道了)

您必须使用
contexthemewrapper
手动膨胀视图:

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" />


我可以,但这很可能会导致一些弗兰肯斯坦控制。我的意思是,最好使用AppCompat的黑暗控件。因为那样的话,所有的东西都会被正确地剥皮。我只是不知道如何在几个控件中使用AppCompat dark,因为ToCompleteTextView尚未设置样式-有关更多详细信息,请参见此处:
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);
<!-- 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" />