简体   繁体   English

EditText背景色

[英]EditText background color

I have an Activity that consists only of a huge EditText, full screen. 我有一个仅包含一个巨大的全屏EditText的Activity。

To match with my app's colours, i changed the background color of the EditText. 为了与我的应用程序的颜色匹配,我更改了EditText的背景颜色。 But although it occupies the whole screen, it only changes the first line. 但是,尽管它占据了整个屏幕,但它仅更改了第一行。 Everytime i press enter i go to a new line and that line gets the background i want. 每次按Enter键,我都会转到新行,并且该行会获取我想要的背景。

What propertie can i set in order to do this? 我可以设置什么属性才能执行此操作? I just can't see it! 我只是看不到!

我将用视图包装EditText并在视图上设置背景色。

为了更改已经带有背景的视图的背景,最好提供自己的图像,然后使用patch9进行适当的调整大小,希望对您有所帮助。

@everyone: @大家:

While searching for a solution for this same kind of problem i found this page. 在寻找解决此类问题的方法时,我找到了此页面。 While reading the solutions i thought to myself: "There must be a better way to do this, instead of making your own styles and widgetborders, etcetera, etcetera, .... 在阅读解决方案时,我对自己心想:“必须有一种更好的方法来做到这一点,而不是制作自己的样式和部件,等等。”

Therefore, i present to you: the 1 LINE OF CODE SOLUTION!!! 因此,我向您介绍:1行代码解决方案!!! No trolling. 没有巨魔。

<your-widget-component-that-has-a-background-color>.getBackground().setColorFilter(Color.<your-desired-color>, PorterDuff.Mode.MULTIPLY);).

It works like this: 它是这样的:

  • "getBackground()" fetches the background from the component “ getBackground()”从组件获取背景
  • "setColorFilter" will call a filtering on the background image itself “ setColorFilter”将在背景图像本身上调用过滤
  • "Color." “颜色。” determines what color you want to pass onto the filter 确定要传递给滤镜的颜色
  • "PorterDuff.Mode." “PorterDuff.Mode。” sets the kind of manipulation you would like to do with the given color and the fetched background image. 设置您要对给定的颜色和所获取的背景图像进行的处理。 People with knowledge of image editing software (GIMP, Photoshop, ...) might recognise these modes. 熟悉图像编辑软件(GIMP,Photoshop等)的人可能会认识到这些模式。 Basically, each mode has a certain effect on how the color is applied to the background image. 基本上,每种模式都会对如何将颜色应用于背景图像产生一定的影响。 to simply "override" the color of the image, while preservind it's gradients, borders and such, use "MULTIPLY" 要简单地“覆盖”图像的颜色,同时保留其渐变,边界等,请使用“ MULTIPLY”

I haven't profoundly read the PorterDuff class documentation, but honestly, ain't it a powerful bit of code? 我没有深入阅读PorterDuff类文档,但是老实说,它不是功能强大的代码吗?

With kind regards, 亲切的问候,

如何设置EditText小部件的背景颜色:

editText.setBackgroundColor(Color.RED);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM