Java 使用 appcompat v7 更改 EditText 底线颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26574328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing EditText bottom line color with appcompat v7
提问by Laurent
I am using appcompat v7 to get the look consistent on Android 5 and less. It works rather well. However I cannot figure out how to change the bottom line color and the accent color for EditTexts. Is it possible?
我正在使用 appcompat v7 在 Android 5 及更低版本上获得一致的外观。它工作得很好。但是我无法弄清楚如何更改 EditTexts 的底线颜色和强调色。是否可以?
I have tried to define a custom android:editTextStyle
(cf. below) but I only succeeded to change the full background color or text color but not the bottom line nor the accent color. Is there a specific property value to use? do I have to use a custom drawable image through the android:background
property? is it not possible to specify a color in hexa?
我试图定义一个自定义android:editTextStyle
(参见下面),但我只成功地改变了完整的背景颜色或文本颜色,而不是底线或强调色。是否有要使用的特定属性值?我必须通过android:background
属性使用自定义的可绘制图像吗?不能在 hexa 中指定颜色吗?
<style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:editTextStyle">@style/Widget.App.EditText</item>
</style>
<style name="Widget.App.EditText" parent="Widget.AppCompat.EditText">
???
</style>
According to android API 21 sources, EditTexts with material design seem to use colorControlActivated
and colorControlNormal
. Therefore, I have tried to override these properties in the previous style definition but it has no effect. Probably appcompat does not use it. Unfortunately, I cannot find the sources for the last version of appcompat with material design.
根据 android API 21 消息来源,具有材料设计的 EditTexts 似乎使用colorControlActivated
和colorControlNormal
. 因此,我尝试在之前的样式定义中覆盖这些属性,但没有效果。可能 appcompat 不使用它。不幸的是,我找不到带有材料设计的 appcompat 最新版本的来源。
采纳答案by Laurent
Finally, I have found a solution. It simply consists of overriding the value for colorControlActivated
, colorControlHighlight
and colorControlNormal
in your app theme definition and not your edittext style. Then, think to use this theme for whatever activity you desire. Below is an example:
最后,我找到了解决方案。它只包括覆盖colorControlActivated
,colorControlHighlight
和colorControlNormal
应用主题定义中的值,而不是您的 edittext 样式。然后,考虑将此主题用于您想要的任何活动。下面是一个例子:
<style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">#c5c5c5</item>
<item name="colorControlActivated">@color/accent</item>
<item name="colorControlHighlight">@color/accent</item>
</style>
回答by Peter
One quick solution for your problem is to look in yourappspackage/build/intermediates/exploded-aar/com.android.support/appcompat-v7/res/drawable/ for abc_edit_text_material.xml and copy that xml file in your drawable folder. Then you can change the colour of the 9 patch files from inside this selector, in order to match your preferences.
您的问题的一种快速解决方案是在 yourappspackage/build/intermediates/exploded-aar/com.android.support/appcompat-v7/res/drawable/ 中查找 abc_edit_text_material.xml 并将该 xml 文件复制到您的 drawable 文件夹中。然后您可以在此选择器中更改 9 个补丁文件的颜色,以匹配您的偏好。
回答by TanmayP
The accepted answer is a bit more per style basis thing, but the most efficient thing to do is to add the colorAccentattribute in your AppTheme style like this:
接受的答案是基于样式的更多内容,但最有效的做法是在 AppTheme 样式中添加colorAccent属性,如下所示:
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorAccent">@color/colorAccent</item>
<item name="android:editTextStyle">@style/EditTextStyle</item>
</style>
<style name="EditTextStyle" parent="Widget.AppCompat.EditText"/>
The colorAccent attribute is used for widget tinting throughout the app and thus should be used for consistency
colorAccent 属性用于整个应用程序的小部件着色,因此应用于一致性
回答by hordurh
I felt like this needed an answer in case somebody wanted to change just a single edittext. I do it like this:
我觉得这需要一个答案,以防有人只想更改一个编辑文本。我这样做:
editText.getBackground().mutate().setColorFilter(ContextCompat.getColor(context, R.color.your_color), PorterDuff.Mode.SRC_ATOP);
回答by l-l
This can be changed in XML by using:
这可以通过使用在 XML 中更改:
For Reference API >= 21 compatibility use:
对于参考 API >= 21 兼容性使用:
android:backgroundTint="@color/blue"
For backward API < 21 compatibility use:
对于向后 API < 21 兼容性使用:
app:backgroundTint="@color/blue"
回答by rz0
If you want change bottom line without using app colors, use these lines in your theme:
如果您想在不使用应用颜色的情况下更改底线,请在您的主题中使用以下几行:
<item name="android:editTextStyle">@android:style/Widget.EditText</item>
<item name="editTextStyle">@android:style/Widget.EditText</item>
I don't know another solution.
我不知道另一种解决方案。
回答by reVerse
While Laurents solutionis correct, it comes with some drawbacks as described in the comments since not only the bottom line of the EditText
gets tinted but the Back Button of the Toolbar
, CheckBoxes
etc. as well.
虽然Laurents的解决方案是正确的,它与作为,因为不仅是底线在评论中描述的一些缺点EditText
被染色,但的后退按钮Toolbar
,CheckBoxes
等也是如此。
Luckily v22.1
of appcompat-v7
introduced some new possibilities. Now it's possible to assign a specific theme only to one view. Straight from the Changelog:
幸运的是v22.1
,appcompat-v7
引入了一些新的可能性。现在可以仅将特定主题分配给一个视图。直接来自更新日志:
Deprecated use of app:theme for styling Toolbar. You can now useandroid:theme for toolbars on all API level 7 and higher devices and android:themesupport for all widgetson API level 11 and higher devices.
不推荐使用 app:theme 样式工具栏。您现在可以将android:theme 用于所有 API 级别 7 和更高级别设备上的工具栏,并为API 级别 11 和更高级别设备上的所有小部件使用android:theme支持。
So instead of setting the desired color in a global theme, we create a new one and assign it only to the EditText
.
因此,我们不是在全局主题中设置所需的颜色,而是创建一个新颜色并将其仅分配给EditText
.
Example:
例子:
<style name="MyEditTextTheme">
<!-- Used for the bottom line when not selected / focused -->
<item name="colorControlNormal">#9e9e9e</item>
<!-- colorControlActivated & colorControlHighlight use the colorAccent color by default -->
</style>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/MyEditTextTheme"/>
回答by Felipe Conde
If you are using appcompat-v7:22.1.0+
you can use the DrawableCompatto tint your widgets
如果您正在使用,appcompat-v7:22.1.0+
您可以使用DrawableCompat为您的小部件着色
public static void tintWidget(View view, int color) {
Drawable wrappedDrawable = DrawableCompat.wrap(view.getBackground());
DrawableCompat.setTint(wrappedDrawable.mutate(), getResources().getColor(color));
view.setBackgroundDrawable(wrappedDrawable);
}
回答by Sfseyhan
You can set background of edittext to a rectangle with minus padding on left, right and top to achieve this. Here is the xml example:
您可以将 edittext 的背景设置为在左侧、右侧和顶部带有减号填充的矩形以实现此目的。这是 xml 示例:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="-1dp"
android:left="-1dp"
android:right="-1dp"
android:bottom="1dp"
>
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#6A9A3A"/>
</shape>
</item>
</layer-list>
Replace the shape with a selector if you want to provide different width and color for focused edittext.
如果您想为焦点编辑文本提供不同的宽度和颜色,请用选择器替换该形状。
回答by ywwynm
Here is a part of source code of TextInputLayout
in support design library(UPDATED for version 23.2.0), which changes EditText
's bottom line color in a simpler way:
这是TextInputLayout
支持设计库的一部分源代码(更新版本 23.2.0),它EditText
以更简单的方式更改了底线颜色:
private void updateEditTextBackground() {
ensureBackgroundDrawableStateWorkaround();
final Drawable editTextBackground = mEditText.getBackground();
if (editTextBackground == null) {
return;
}
if (mErrorShown && mErrorView != null) {
// Set a color filter of the error color
editTextBackground.setColorFilter(
AppCompatDrawableManager.getPorterDuffColorFilter(
mErrorView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
}
...
}
It seems that all of above code become useless right now in 23.2.0 if you want to change the color programatically.
如果您想以编程方式更改颜色,似乎上述所有代码现在在 23.2.0 中都变得无用了。
And if you want to support all platforms, here is my method:
如果你想支持所有平台,这是我的方法:
/**
* Set backgroundTint to {@link View} across all targeting platform level.
* @param view the {@link View} to tint.
* @param color color used to tint.
*/
public static void tintView(View view, int color) {
final Drawable d = view.getBackground();
final Drawable nd = d.getConstantState().newDrawable();
nd.setColorFilter(AppCompatDrawableManager.getPorterDuffColorFilter(
color, PorterDuff.Mode.SRC_IN));
view.setBackground(nd);
}