Android line break (\ n) shows an unhealthy problem in TextView

Source: Internet
Author: User

Problem description

In Android development, this is the case when you use TextView to set a newline:

1. If you write "aaaaa\nbbbb" directly in the XML file, you can wrap it, and display it as:

Aaaaa
bbbbb

2, in the Java file with Textviewobj.settext ("AAAA\NBBBB") can also be wrapped, shown as:

Aaaaa
bbbbb

3. When "AAAAA\NBBBB" is encapsulated in a property of a string type in a class, use Textview.settext (the object name of the class) in Java code. Shown as:

aaaaa\nbbbb


The 3rd above is the problem, not the normal line-wrapping.

Problem Analysis:

Very simply, when the program is running to show "AAAA\NBBBB", then you must set the TextView "AAAA\\NBBBB",
There is a "\" Here, the escape of the string that the object carries has been eliminated,

After passing an object in the following way, if the object contains a string member variable, the escape character contained in the string is invalidated. Because Android is already in the hands and feet of the delivery.

Intent.putextra (extra_object,people);

For example, passing a People object, which has a member variable description that describes the people in detail,
And this description member, which is a string containing the "\ n" escape character, when it is passed,
Its escape character "\ n" has become "\\n",
So, what we're going to do is, after the object is passed, convert it over.

People people= (People) Intent.getserializableextra (Extra_object);

String temp = people.getdescription (). Replace ("\\n", "\ n");
People.setdescription (temp);

This way, then put on the TextView, you can normally display the line break.

Android line break (\ n) shows an unhealthy problem in TextView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.