简体   繁体   English

每行设置Edittext背景

[英]Set Edittext Background per Line

I would like to set the background of edittext per line as opposed to android's block style of setting up the background ie when a user writes some text, I should get the text entered by a user, split it into lines and set a background color for each line. 我想每行设置edittext的背景,而不是android设置背景的块样式,即当用户写一些文本时,我应该让用户输入文本,将其分成几行并为每行。 Check the attached picture for references. 检查所附图片以供参考。 I've not been able to figure out how to achieve the same, please help. 我无法弄清楚该如何实现,请帮忙。 This is what I've done so far, thanks in advance. 这是我到目前为止所做的事情,在此先感谢您。 Desired picture [1]: https://i.stack.imgur.com/LhpTQ.jpg 所需图片[1]: https//i.stack.imgur.com/LhpTQ.jpg

 String allLines = userInput.getText().toString(); // userInput is an edittext field
        String[] lines;
        String linelimit =  "\n";

        lines= allLines.split(linelimit);

        //this gives the lines in the user input text][1]][1]

Use 2 or more separate TextView/EditText instead. 请改用2个或更多单独的TextView / EditText。 If number of lines are different during execution then add Textview/EditText dynamically in LinearLayout. 如果执行期间的行数不同,则在LinearLayout中动态添加Textview / EditText。

It is not possible in any view (that I know of) because there is only one background for every view, or EditText and cannot be separated for each line. 在任何视图(据我所知)中都是不可能的,因为每个视图或EditText都只有一个背景,并且每一行都不能分开。

If you are using an EditText that user can edit, then you will have to calculate width of EditText as text changes and if its width exceeds a limit, you can add another EditText below it. 如果使用的是用户可以编辑的EditText,则必须在文本更改时计算EditText的宽度,如果宽度超过限制,则可以在其下添加另一个EditText。

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

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