Risposte:
Potresti fondamentalmente usare tag html nella tua risorsa stringa come:
<resource>
<string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string>
</resources>
E usa Html.fromHtml o usa spannable, controlla il link che ho pubblicato.
Vecchia domanda simile: è possibile avere più stili all'interno di un TextView?
Html.fromHtml(): ... <![CDATA[<b><i>so</i></b>]]>...
getText(R.string.whatever)piuttosto chegetString(R.string.whatever)
nameinvece di id?
Html.fromHtmlo Spannable. Usa getText(R.string.whatever)come menzionato @andygeers.
Usa tag html all'interno delle risorse di stringa: -
<resources>
<string name="string_resource_name"><![CDATA[<b> Your text </b>]]> </string>
</resources>
E ottieni testo in grassetto da risorse stringa come: -
private Spanned getSpannedText(String text) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT);
} else {
return Html.fromHtml(text);
}
}
String s = format(context.getResources().getString(R.string.string_resource_name));
textView.setText(getSpannedText(s));
Come ha detto David Olsson, puoi usare HTML nelle tue risorse di stringa:
<resource>
<string name="my_string">A string with <i>actual</i> <b>formatting</b>!</string>
</resources>
Quindi se usi getText(R.string.my_string)piuttosto che getString(R.string.my_string)ricevi un CharSequenceinvece di un Stringche contiene la formattazione incorporata.
In kotlin, puoi creare funzioni di estensioni sulle risorse (attività | frammenti | contesto) che convertiranno la tua stringa in un intervallo html
per esempio
fun Resources.getHtmlSpannedString(@StringRes id: Int): Spanned = getString(id).toHtmlSpan()
fun Resources.getHtmlSpannedString(@StringRes id: Int, vararg formatArgs: Any): Spanned = getString(id, *formatArgs).toHtmlSpan()
fun Resources.getQuantityHtmlSpannedString(@PluralsRes id: Int, quantity: Int): Spanned = getQuantityString(id, quantity).toHtmlSpan()
fun Resources.getQuantityHtmlSpannedString(@PluralsRes id: Int, quantity: Int, vararg formatArgs: Any): Spanned = getQuantityString(id, quantity, *formatArgs).toHtmlSpan()
fun String.toHtmlSpan(): Spanned = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(this, Html.FROM_HTML_MODE_LEGACY)
} else {
Html.fromHtml(this)
}
uso
//your strings.xml
<string name="greeting"><![CDATA[<b>Hello %s!</b><br>]]>This is newline</string>
//in your fragment or activity
resources.getHtmlSpannedString(R.string.greeting, "World")
MODIFICA ancora più estensioni
fun Context.getHtmlSpannedString(@StringRes id: Int): Spanned = getString(id).toHtmlSpan()
fun Context.getHtmlSpannedString(@StringRes id: Int, vararg formatArgs: Any): Spanned = getString(id, *formatArgs).toHtmlSpan()
fun Context.getQuantityHtmlSpannedString(@PluralsRes id: Int, quantity: Int): Spanned = resources.getQuantityString(id, quantity).toHtmlSpan()
fun Context.getQuantityHtmlSpannedString(@PluralsRes id: Int, quantity: Int, vararg formatArgs: Any): Spanned = resources.getQuantityString(id, quantity, *formatArgs).toHtmlSpan()
fun Activity.getHtmlSpannedString(@StringRes id: Int): Spanned = getString(id).toHtmlSpan()
fun Activity.getHtmlSpannedString(@StringRes id: Int, vararg formatArgs: Any): Spanned = getString(id, *formatArgs).toHtmlSpan()
fun Activity.getQuantityHtmlSpannedString(@PluralsRes id: Int, quantity: Int): Spanned = resources.getQuantityString(id, quantity).toHtmlSpan()
fun Activity.getQuantityHtmlSpannedString(@PluralsRes id: Int, quantity: Int, vararg formatArgs: Any): Spanned = resources.getQuantityString(id, quantity, *formatArgs).toHtmlSpan()
fun Fragment.getHtmlSpannedString(@StringRes id: Int): Spanned = getString(id).toHtmlSpan()
fun Fragment.getHtmlSpannedString(@StringRes id: Int, vararg formatArgs: Any): Spanned = getString(id, *formatArgs).toHtmlSpan()
fun Fragment.getQuantityHtmlSpannedString(@PluralsRes id: Int, quantity: Int): Spanned = resources.getQuantityString(id, quantity).toHtmlSpan()
fun Fragment.getQuantityHtmlSpannedString(@PluralsRes id: Int, quantity: Int, vararg formatArgs: Any): Spanned = resources.getQuantityString(id, quantity, *formatArgs).toHtmlSpan()
strings.xml
<string name="my_text"><Data><![CDATA[<b>Well Done !</b><br></br>All of your activities are completed.<br></br>You may now close the app.<br></br>See you again next time.]]></Data></string>
Impostare
textView.setText(Html.fromHtml(getString(R.string.activity_completed_text)));
Puoi farlo da stringa
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="total_review"><b>Total Review: </b> </string>
</resources>
e puoi accedervi dal codice java come
proDuctReviewNumber.setText(getResources().getString(R.string.total_review)+productDetailsSuccess.getProductTotalReview());
strings.xml
<string name="sentence">This price is <b>%1$s</b> USD</string>
page.java
String successMessage = getString(R.string.message,"5.21");
Questo prezzo è di 5,21 USD