Ecco la maggior parte degli esempi. Non credo che il pre
tag sia supportato.
Questo è il strings.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Formatting</string>
<string name="link"><b>Hello World</b> This is a test of the URL <a href="http://www.example.com/">Example</a></string>
<string name="bold"><b>This text is bold</b></string>
<string name="emphasis"><em>This text is emphasized</em></string>
<string name="sup">This is <sub>subscript</sub> and <sup>superscript</sup></string>
</resources>
Ecco il layout. Nota affinché il collegamento sia effettivamente cliccabile, è necessario un po 'di lavoro extra:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/test1"
android:linksClickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/test2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/test3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/test4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
</ScrollView>
Infine, il codice:
TextView test1 = (TextView)findViewById(R.id.test1);
Spanned spanned = Html.fromHtml(getString(R.string.link));
test1.setMovementMethod(LinkMovementMethod.getInstance());
test1.setText(spanned);
TextView test2 = (TextView)findViewById(R.id.test2);
test2.setText(Html.fromHtml(getString(R.string.bold)));
TextView test3 = (TextView)findViewById(R.id.test3);
test3.setText(Html.fromHtml(getString(R.string.emphasis)));
TextView test4 = (TextView)findViewById(R.id.test4);
test4.setText(Html.fromHtml(getString(R.string.sup)));