android:theme =“@ style / ...”doenst采取背景

时间:2018-01-18 21:41:39

标签: android android-layout

我获得了编辑文本的以下背景:

edit_text_rounded_white.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <solid android:color="#66c6c6c6"/>
        <stroke android:width="0.1dp"
            android:color="#66c6c6c6"/>
       <corners android:radius="40dp"/>
    </shape>
</item>
</selector>

在我的styles.xml中我定义了以下内容:

<style name="editText">
    <item name="android:background">@drawable/edit_text_rounded_white</item>
    <item name="android:drawablePadding">5dp</item>
    <item name="android:paddingBottom">2dp</item>
    <item name="android:paddingTop">2dp</item>
    <item name="android:paddingLeft">10dp</item>
    <item name="android:paddingRight">10dp</item>
    <item name="android:textCursorDrawable">@drawable/edit_text_cursor</item>
    <item name="colorControlNormal">@color/colorPrimaryDark</item>
    <item name="colorControlActivated">@color/transparentWhite50</item>
</style>

当我通过:style="@style/editText"为我的editTexts设置样式时它会占用背景,但不会采用我设置的colorControl。当我通过android:theme="@style/editText"设置样式时,它会采用colorControls但不会显示背景图像。我已经在样式文件中尝试了srcwindowBackground而不是background

任何想法如何实现两者?显示背景和colorControls?

1 个答案:

答案 0 :(得分:0)

将你的风格分成两部分:

<style name="editTextStyle">
    <item name="android:background">@drawable/edit_text_rounded_white</item>
    <item name="android:drawablePadding">5dp</item>
    <item name="android:paddingBottom">2dp</item>
    <item name="android:paddingTop">2dp</item>
    <item name="android:paddingLeft">10dp</item>
    <item name="android:paddingRight">10dp</item>
    <item name="android:textCursorDrawable">@drawable/edit_text_cursor</item>
</style>

<style name="editTextTheme">
    <item name="colorControlNormal">@color/colorPrimaryDark</item>
    <item name="colorControlActivated">@color/transparentWhite50</item>
</style>

然后将两者应用于您的观点:

<EditText
    ...
    android:theme="@style/editTextTheme"
    style="@style/editTextStyle"/>