Java 如何在 4.0+ 上更改 Edittext 下划线的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18571816/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:06:48  来源:igfitidea点击:

How change color of the underline of Edittext on 4.0+?

javaandroidcolorsandroid-edittext

提问by silvia_aut

I have on my app Theme.Holo but I want to change the color of the underline border of edittext.

我在我的应用程序 Theme.Holo 上有,但我想更改 edittext 的下划线边框的颜色。

I don't want a full border around the edittext. I just want to change the color of the edittext layout on version 4.0+ .

我不想要编辑文本周围的完整边框。我只想在 4.0+ 版本上更改 edittext 布局的颜色。

How can I do this?

我怎样才能做到这一点?

采纳答案by user1582281

I think you can change android:background to your desired drawable to replace your edit text's default background.

我认为您可以将 android:background 更改为您想要的 drawable 来替换您编辑文本的默认背景。

回答by Peregreen

You could use 9patch. This resource is wonderful example: android holo

你可以用9patch。这个资源是很好的例子:android holo

回答by SIr Codealot

int color = Color.parse("#HEX")
editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);

回答by Visionwriter

For those that want a more complete answer. The default color is actually a 9- patch background. So you will need to replace it.

对于那些想要更完整答案的人。默认颜色实际上是 9 色块背景。所以你需要更换它。

Go to http://android-holo-colors.com/. Here you will see a category called "color".

转到http://android-holo-colors.com/。在这里,您将看到一个名为“颜色”的类别。

Click on the colored square beside it and you will see a color swatch come up. Change the color to that desired.

单击旁边的彩色方块,您会看到一个色样出现。将颜色更改为所需的颜色。

Scroll down and find another category called EditText. Click the yes button beside it.

向下滚动并找到另一个名为 EditText 的类别。单击它旁边的“是”按钮。

Finally scroll down to the end of the page and download the generated zip.

最后向下滚动到页面末尾并下载生成的 zip。

Decompress the zip on your computer. You will find a couple of drawable folders according to the different pixel densities. Copy them to the appropriate folders in your project.

在您的计算机上解压缩 zip。您会根据不同的像素密度找到几个可绘制文件夹。将它们复制到项目中的相应文件夹中。

There will also be a folder simply called "drawable". In it will be an xml file. If you have a folder called "drawable", different from "drawable-mdpi, drawable-hdpi, etc", copy the xml file to the "drawable" folder, else create one in your "res" folder

还有一个文件夹,简称为“drawable”。它将是一个 xml 文件。如果您有一个名为“drawable”的文件夹,不同于“drawable-mdpi、drawable-hdpi 等”,请将 xml 文件复制到“drawable”文件夹中,否则在“res”文件夹中创建一个

Lastly, copy the name of the xml file. Go to the EditText you would like to change and set the xml file as your background. If your xml file was titled edit_text_holo_light, the xml code you would put in your EditText would be:

最后,复制 xml 文件的名称。转到您想要更改的 EditText 并将 xml 文件设置为您的背景。如果您的 xml 文件名为 edit_text_holo_light,则您将放入 EditText 的 xml 代码将是:

android:background="@drawable/edit_text_holo_light

Save the project and run.

保存项目并运行。

回答by Soheil Setayeshi

use android:backgroundTint=""in your EditText xml layout

使用android:backgroundTint=""您的EditText上的XML布局