How to make XML strings bold, underlined etc?

2020-08-10 06:17发布

问题:

http://docs.fusioncharts.com/charts/contents/Styles/Font.html

I tried this, along with a lot of things but failed to do so. Here's what I want.

<string name="ss">Bold. Underlined. Italic. Big. Small</string>

I want to format a little bit of the string. Where it's written bold, I want it to be bold...and same for others.

I tried a lot of tags ...but well nothing worked, and I couldn't find anything on Google or SO.

I know how to do it in a textview, but that's not what I want...

I'm sending some text resource to an activity that shows it... If I did it with different text views, I'd have to create several of them, a new one for whenever I want bold text, and that's not very elegant.

Is there a way to simple do this in the XML file ? or some other way ?

回答1:

Try wrapping your marked up text in CDATA tags. For example:

<string name="ss"><![CDATA[<b>Bold.</b> <u>Underlined.</u> <i>Italic.</i> <big>Big.</big> <small>Small</small>]]></string>

And then use Html.fromHtml wherever you're wanting to display it:

Html.fromHtml(getString(R.string.ss))


回答2:

This problem has been driving me crazy for ages. It's something sooo simple that you just want it to work!!!

Anyway I've found an answer here at http://www.coderzheaven.com/2011/06/19/styling-text-in-android-through-xml/

The key is to load the resource as a CharSequence using getResources().getText(R.string.xxxx) this will retain all the style information and allow you to use inline styling tags. My mistake was using getString() because when loading your resource getString() will cause the string to lose all its style information.



回答3:

exemple:

<string name="ss"><font size="15"><b>Parrainage</b></font><u>subscribe</u></string>

b = bold et u = underline .....etc



回答4:

This is working for me.

<string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>

txt.setText(Html.fromHtml(getString(R.string.welcome_messages)));

more details check Official site: https://developer.android.com/guide/topics/resources/string-resource.html#StylingWithSpannables



回答5:

in dimens file write:

<dimen name="size_edittext">180dp</dimen>

and in your xml layout or activity call it:

android:@dimen/ size_edittext