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

时间:2014-10-30 01:29:36

标签: android android-edittext spinner android-appcompat material-design

我在使用AppCompat v21主题创建Material Design应用时出现问题。首先,我使用带有深色操作栏的AppCompat灯来获取我的应用主题。

<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变体的原因。我的问题是,我有几个布局,我希望在布局中放置SpinnerEditText,背景是我的theme_color,它是黑暗的。这会在深色背景上产生带有黑色的Spinner/EditText。如何使这些控件的样式/主题使用Dark变体,以便它们显示白色文本而不是黑色文本。更糟糕的是,我需要微调器显示白色文本,但需要显示微调器的下拉项以显示光照版本。

3 个答案:

答案 0 :(得分:0)

我在深色背景的工具栏中使用Spinner,并记得Google I / O Android App也是如此,所以我查看了它的样式 - 更具体地说,ActionBarThemeOverlay here

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

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

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

答案 1 :(得分:0)

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

您必须使用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);

要获得inflater的布局资源,只需创建一个单视图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" />

答案 2 :(得分:-1)

这个怎么样..

为你创建一个样式小部件,例如:

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

然后在你的xml中将此属性添加到EditText

style="@style/CustomEditText"