Android 如何更改 EditText 中的线条颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24677414/
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
How to change line color in EditText
提问by Alex
I am creating an EditText in my layout xml file
我在我的布局 xml 文件中创建一个 EditText
But I want to change color line in EditText from Holo to (for example) red. How that can be done?
但我想将 EditText 中的颜色线从 Holo 更改为(例如)红色。怎么做?
回答by MilapTank
This is the best tool that you can use for all views and its FREE many thanks to @Jér?me Van Der Linden.
这是您可以用于所有视图的最佳工具,而且非常感谢@ Jér?me Van Der Linden,它是免费的。
The Android Holo Colors Generator allows you to easily create Android components such as EditText
or spinner with your own colours for your Android application. It will generate all necessary nine patch assets plus associated XML drawable and styles which you can copy straight into your project.
Android Holo 颜色生成器允许您轻松地EditText
为您的 Android 应用程序创建具有您自己的颜色的Android 组件,例如或微调器。它将生成所有必需的九个补丁资产以及相关的 XML drawable 和样式,您可以将它们直接复制到您的项目中。
UPDATE 1
更新 1
This domain seems expired but the project is an open source you can find here
此域似乎已过期,但该项目是一个开源项目,您可以在此处找到
try it
尝试一下
this image put in the background of EditText
这张图片放在背景中 EditText
android:background="@drawable/textfield_activated"
UPDATE 2
更新 2
For API 21 or higher, you can use android:backgroundTint
对于 API 21 或更高版本,您可以使用 android:backgroundTint
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
android:backgroundTint="@android:color/holo_red_light" />
Update 3Now We have with back support AppCompatEditText
更新 3现在我们有支持AppCompatEditText
Note:We need to use app:backgroundTintinstead of android:backgroundTint
注意:我们需要使用app:backgroundTint而不是android:backgroundTint
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
app:backgroundTint="@color/blue_gray_light" />
回答by Mladen Rakonjac
I don't like previous answers. The best solution is to use:
我不喜欢以前的答案。最好的解决方案是使用:
<android.support.v7.widget.AppCompatEditText
app:backgroundTint="@color/blue_gray_light" />
android:backgroundTint
for EditText
works only on API21+. Because of it, we have to use the support library and AppCompatEditText
.
android:backgroundTint
对于EditText
只在作品API21 +。因此,我们必须使用支持库和AppCompatEditText
.
Note: we have to use app:backgroundTint
instead of android:backgroundTint
注意:我们必须使用app:backgroundTint
代替android:backgroundTint
AndroidX version
安卓X版本
<androidx.appcompat.widget.AppCompatEditText
app:backgroundTint="@color/blue_gray_light" />
回答by Damian Esteves
You can also quickly change the EditText's underline color by tinting the background of the EditText like so:
您还可以通过为 EditText 的背景着色来快速更改 EditText 的下划线颜色,如下所示:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Something or Other"
android:backgroundTint="@android:color/holo_green_light" />
回答by Rahul
for APIbelow 21, you can use theme attribute in EditText
put below code into style file
对于低于 21 的API,您可以在EditText
将下面的代码放入样式文件中使用主题属性
<style name="MyEditTextTheme">
<item name="colorControlNormal">#FFFFFF</item>
<item name="colorControlActivated">#FFFFFF</item>
<item name="colorControlHighlight">#FFFFFF</item>
</style>
use this style in EditText
as
使用这种风格EditText
作为
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="@dimen/user_input_field_height"
android:layout_marginTop="40dp"
android:hint="@string/password_hint"
android:theme="@style/MyEditTextTheme"
android:singleLine="true" />
回答by doctorram
Programmatically, you can try:
以编程方式,您可以尝试:
editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_red_light), PorterDuff.Mode.SRC_ATOP);
回答by j2emanue
i think the best way is by theme:
我认为最好的方法是按主题:
<style name="MyEditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">@color/black</item>
<item name="colorControlActivated">@color/action_blue</item>
<item name="colorControlHighlight">@color/action_blue</item>
</style>
<style name="AddressBookStyle" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">13sp</item>
<item name="android:theme">@style/MyEditTextTheme</item>
</style>
<android.support.v7.widget.AppCompatEditText
style="@style/AddressBookStyle"/>
回答by Suhyun Kim
To change Edittext's underline color:
要更改 Edittext 的下划线颜色:
If you want the entire app to share this style, then you can do the following way.
如果你想让整个 App 都共享这种风格,那么你可以通过以下方式进行。
(1) go to styles.xml file. Your AppTheme that inherits the parent of Theme.AppCompat.Light.DarkActionBar (in my case) will be the base parent of all they style files in your app. Change the name of it to “AppBaseTheme'. Make another style right under it that has the name of AppTheme and inherits from AppBaseTheme that you just edited. It will look like following:
(1) 转到styles.xml 文件。继承 Theme.AppCompat.Light.DarkActionBar 父级的 AppTheme(在我的例子中)将是你的应用程序中所有样式文件的基本父级。将其名称更改为“AppBaseTheme”。在它下面创建另一个样式,名称为 AppTheme,并继承自您刚刚编辑的 AppBaseTheme。它将如下所示:
<!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<!--see http://www.google.com/design/spec/style/color.html#color-color-palette-->
<item name="colorPrimary">@color/material_brown_500</item>
<item name="colorPrimaryDark">@color/material_brown_700</item>
<item name="colorAccent">@color/flamingo</item>
<style name="AppTheme" parent="AppBaseTheme">
<!-- Customize your theme here. -->
</style>
Then change the “colorAccent” to whatever the color you want your EditText line color to be.
然后将“colorAccent”更改为您希望 EditText 线条颜色的任何颜色。
(2) If you have other values folders with style.xml, this step is very important. Because that file will inherit from your previous parent xml file. For example, I have values-19/styles.xml. This is specifically for Kitkat and above. Change its parent to AppBaseTheme and make sure to get rid of “colorAccent” so that it doesn't override the parent's color. Also you need to keep the items that are specific to version 19. Then it will look like this.
(2)如果你有其他带有style.xml的values文件夹,这一步很重要。因为该文件将从您以前的父 xml 文件继承。例如,我有 values-19/styles.xml。这是专门针对 Kitkat 及以上版本的。将其父项更改为 AppBaseTheme 并确保摆脱“colorAccent”,以便它不会覆盖父项的颜色。您还需要保留特定于版本 19 的项目。然后它看起来像这样。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
回答by Krishna Raj Salim
Its pretty simple (Required: Minimum API 21)...
它非常简单(必需:最低 API 21)...
- Go to your xml and select the EditText field
- On the right side, you can see the 'Attributes' window. Select 'View All Attributes'
- Just search for 'tint'
- And add/change the 'backgroundTint' to a desired color hex (say #FF0000)
- 转到您的 xml 并选择 EditText 字段
- 在右侧,您可以看到“属性”窗口。选择“查看所有属性”
- 只需搜索“色调”
- 并将“backgroundTint”添加/更改为所需的颜色十六进制(例如#FF0000)
Keep Coding........ :)
继续编码........ :)
回答by Egor Neliuba
The line's color is defined by EditText
's background property. To change it you should change the android:background
in the layout file.
线条的颜色由EditText
的背景属性定义。要更改它,您应该更改android:background
布局文件中的 。
I should note that this style is achieved by using a 9-patch drawable. If you look in the SDK, you can see that the background of the EditText
is this image:
我应该注意到这种风格是通过使用 9-patch drawable 实现的。如果你查看SDK,你可以看到它的背景EditText
是这张图片:
To change it you could open it in an image manipulation program and color it in desired color. Save it as bg_edit_text.9.png
and then put it in you drawable folder. Now you can apply it as a background for your EditText
like so:
要更改它,您可以在图像处理程序中打开它并将其着色为所需的颜色。将其另存为bg_edit_text.9.png
,然后将其放入可绘制文件夹中。现在你可以将它作为你EditText
喜欢的背景:
android:background="@drawable/bg_edit_text"
回答by Matin Ashtiani
You can change the color of EditText programmatically just using this line of code:edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));
您可以使用以下代码行以编程方式更改 EditText 的颜色:edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));