سؤال

I want to display help dialog box in which it has one textview and it loads the content from the String.xml file. Instead of making it one boring paragraphs, I would like to add some formatting to that String.xml For example coloring some sentences, bold..etc. Is there a way I can do that in the xml file within the string?

My xml looks like that

 <string name="help_summary">Clicking on button (Summary) will result in ((report))</string>

So I want (Summary) to be red color and ((report)) to be bold.

How can I achieve that?

هل كانت مفيدة؟

المحلول

You can use Html. When you load the string in the textview use:

yourTextView.setText(Html.fromHtml(context.getResources().getString(R.string.help_summary)));

And use html in the string, for example:

<string name="help_summary"><![CDATA[Clicking on button <span style="color:red">Summary</span> will result in <b>report</b>]]></string>

نصائح أخرى

There are some attributes that can be used in string resources without implementing HTML into and or altering your java code. None needed, you CAN do most of what you seem to need in strings.xml Example:

<string name ="my_string"><i>italics</i><b>bold</b><u>underline</u><font fgcolor="#FFFFFFFF">color</font><small>small text</small></string>

I believe there is a <large/> or <big/> tag as well, amongst a few more.

Edit also, there is \n for a new line. Just include that in your string like so:

<string name="paragraph">Text is one first line \n now text is on next line. \n\n  this will appear as an indented paragraph now.</string>

Hope this helps, happy coding!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top