简体   繁体   English

如何覆盖 Edittext 的 Theme.AppCompat.Light.DarkActionBar 主题

[英]How to override Theme.AppCompat.Light.DarkActionBar theme for edittext

I am trying to create a style for EditText (MyEditText), which inherits from base theme and added extra property settings.我正在尝试为 EditText (MyEditText) 创建一个样式,它继承自基本主题并添加了额外的属性设置。

<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
    <item name="homeAsUpIndicator">@drawable/ic_back_indicator</item>
</style>

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
<style name="MyEditText" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:background">#dbffb1</item>
</style>

When applied MyEditText style to EditText control, existing styles (from theme) seems to be overridden (like line under EditText vanished).当将 MyEditText 样式应用于 EditText 控件时,现有样式(来自主题)似乎被覆盖(例如 EditText 下的行消失了)。 Same thing happens when background is changed for EditText in the layout.在布局中为 EditText 更改背景时,也会发生同样的事情。

I am using targetSdkVersion 23. I like to know what is the proper method to override a single property from default theme.我正在使用 targetSdkVersion 23。我想知道从默认主题覆盖单个属性的正确方法是什么。

Here is the code for EditText in layout (as asked by @3amoura):这是布局中 EditText 的代码(如@3amoura所问):

<EditText android:layout_width="match_parent"
    android:layout_height="82dp"
    android:id="@+id/et_description"
    android:inputType="textMultiLine|textLongMessage"
    android:lines="10"
    android:hint="Describe your complaint in details"
    android:gravity="top"
    style="@style/MyEditText" />

The style of MyEditText should inherit from Widget.AppCompat.EditText instead of Theme.AppCompat.Light.DarkActionBar such that it will be MyEditText的风格应该继承Widget.AppCompat.EditText代替Theme.AppCompat.Light.DarkActionBar这样,这将是

<style name="MyEditText" parent="Widget.AppCompat.EditText">
<item name="android:background">#dbffb1</item>

Then you use the style in the EditText xml然后你使用 EditText xml 中的样式

Edited已编辑

If you are only trying to add a color of the Line drawn -> setBackgroundTint如果您只是尝试添加绘制的线条的颜色 -> setBackgroundTint

If you want to change both the background color and the line color, then custom layer-list or selector will be the only option.如果您想同时更改背景颜色和线条颜色,则自定义图层列表或选择器将是唯一的选择。 A sample layer-list示例图层列表

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="#dbffb1" /> <!-- background color-->
    </shape>
</item>

<item
    android:top="-2dp"
    android:right="-2dp"
    android:left="-2dp">
    <shape>
        <solid android:color="@android:color/transparent" />
        <stroke
            android:width="1dp"
            android:color="@color/colorAccent" />  <!-- color of stroke -->
    </shape>
</item>
</layer-list>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到资源 - Theme.AppCompat.Light.DarkActionBar - No resource found - Theme.AppCompat.Light.DarkActionBar Theme.AppCompat.Light.DarkActionBar - 找不到资源 - Theme.AppCompat.Light.DarkActionBar - No resource found 如何更改textview的默认颜色(Theme.AppCompat.Light.DarkActionBar) - How to change textview default color (Theme.AppCompat.Light.DarkActionBar) 将android:Theme.Light更改为Theme.AppCompat.Light.DarkActionBar - change android:Theme.Light to Theme.AppCompat.Light.DarkActionBar Base.Theme.AppCompat.Light.DarkActionBar与Theme.AppCompat.Light.DarkActionBar - Base.Theme.AppCompat.Light.DarkActionBar vs Theme.AppCompat.Light.DarkActionBar 如何在Theme.AppCompat.Light.DarkActionBar主题中更改菜单项的文本颜色? - How to change the text color of a menu item in Theme.AppCompat.Light.DarkActionBar theme? 我如何像 Theme.AppCompat.Light.DarkActionBar 一样设置 appcompat-v7 工具栏的样式? - How do I style appcompat-v7 Toolbar like Theme.AppCompat.Light.DarkActionBar? Theme.AppCompat.Light.DarkActionBar未显示操作栏 - Theme.AppCompat.Light.DarkActionBar is not showing Action bar Theme.AppCompat.Light.DarkActionBar提供黑色操作栏标题文本 - Theme.AppCompat.Light.DarkActionBar giving black actionbar title text 操作栏未显示:首选项活动中的Theme.Appcompat.Light.DarkActionBar - Action bar not showing :Theme.Appcompat.Light.DarkActionBar in preference activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM