问题
I have one server response for an API request as shown below.
Success! Your request has been sent.\n\nWe’ll inform you once it is done.
This message I need to show in a Snackbar. I need new line to be added in the place of \n
in this response . I tried by using replaceAll
like
String message = (serverResponse.getMessage()).replaceAll("\\n", System.getProperty("line.separator"));
but it is showing like this
Same message if I add in string.xml
resource file and get using getString(R.string.message)
then the \n
is working properly. How can I get a new line from this response string?
I tried changing \n
with other character like <new_line>
from server response and it is working fine with replaceAll
. Problem is only with \n
in response message. Is there any way to parse \n
?
回答1:
What you need is
String message = serverResponse.getMessage().replaceAll("\\\\n", "\n");
Why four backslashes are needed?
Because in Java backslash \
is escape character. If you want to have single backslash literal inside Java string you have to escape it and use \\
But, replaceAll
method expects regex expression, where again backslash is escape character so you need to escape it, too.
Basically, in above code Java string parser will first convert those four backslashes to two \\\\ -> \\
and then regex parser will interpret remaining two backslashes as single backslash literal.
回答2:
I believe you should be able to accomplish by doing the following.
// Replace "\n" with "<br>"
String message = (serverResponse.getMessage()).replaceAll("\\n", "<br>");
// Now set SnackBar text using HTML
mSnackBar.setText(HTML.fromHTML(message))
By using HTML.fromtHTML(String)
you should be able to keep any formatting, such as breaks, ASCII HTML characters (bullets, stars, ect.), coloring and/or bolding/italicizing! I use this quite often to format text in TextViews that I have displayed to users. Do not see why it wouldn't work with SnackBars!
回答3:
The Support Design Library will force only 2 lines for the Snackbar
. This correlates to around 80dp max size.
Your solution should work, and is correct. Try it out in a Toast for a quick test. It will work as your expect. Another test you can do is to get rid of one of the \n
, then it will probably display correctly; however, there are a few other options you can do for. Again, these are just tests. Check below for some real solutions!
Solutions
Remove all the
\n
from theSnackbar
text. This is probably the best solution as it will allow your design to remain as close to Material as possible. Highly RecommendedYou can get the actual
TextView
from theSnackbar
, and modify its max number of lines
View sbv = snackbar.getView(); TextView tv = (TextView) sbv.findViewById(android.support.design.R.id.snackbar_text); tv.setMaxLines(5);
- In XML, you can modify the attribute that affects the Design Library's
Snackbar
number of lines. Not Recommended at all. This name can change without notice and break your UI
<integer name="design_snackbar_text_max_lines">5</integer>
Edit
If you have access to modify the contents of the server response, then I Highly highly highly suggest that you modify the returned server response to be way more concise to the user. Your current message is not concise and takes the user longer than needed to read.
Change it to this..
Request sent! You will be informed shortly.
I would actually find a better work for Request
if you can. For example, if they ordered pizza and sent a request, then you ould say Order sent! ...
. Also, you might need to modify shortly
to be more accurate to what a user can expect. Shortly, to me, means I should expect something within the hour at the very latest.
Anyways, check out this documentation. It is higly recommended for writing styles on Android. https://www.google.com/design/spec/style/writing.html#writing-language Source: Android Multiline Snackbar
回答4:
Snackbar snackbar = Snackbar.make(ref_id, "Success! Your request has been sent.\n\nWe’ll inform you once it is done.",
Snackbar.LENGTH_LONG).setDuration(Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
TextView tv= (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
tv.setMaxLines(3);
snackbar.show();
回答5:
How about this, Jrd.
strings.xml :
<string name="br">\n</string>
snackbar :
"Your request has been sent.." + getResources().getString(R.string.br)
回答6:
StringBuilder strAppend = new StringBuilder(); strAppend.append("\n");
String newString = oldString.replace("\n", strAppend);
Log.d(TAG, "new: " + newString );
来源:https://stackoverflow.com/questions/34417581/how-to-replace-the-characher-n-as-a-new-line-in-android