Android - Deux phrases, deux styles, un TextView
Question
Je tyring d'afficher une TextView qui contient deux phrases et je veux qu'elles soient l'une après l'autre comme ceci:
AAAAAA: BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBB
où un ... est le premier mot partie de la phrase et b ... est une deuxième phrase.
La taille d'un ... & b ... n'est pas constante.
J'ai essayé de faire ceci:
<RelativeLayout
style="@style/f13"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/first_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAAA: " />
<TextView
style="@style/f15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
android:layout_toRightOf="@id/first_text" />
</RelativeLayout>
Mais j'ai eu:
AAAAAA: BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBB
Et je veux que la deuxième (b ...) une phrase se poursuive en dessous de la première phrase (a ...) Je devrais ajouter cette phrase A ... & b ... avoir des styles différents.
Le style pour A:
<style
name="f13">
<item name="android:typeface">sans</item>
<item name="android:textSize">15dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/grey_dark_emperor</item>
</style>
Le style pour b:
<style
name="f15">
<item name="android:typeface">sans</item>
<item name="android:textSize">17dp</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">@color/grey_dark_emperor</item>
</style>
Des pensées ?
La solution
Vous pouvez essayer d'utiliser le Spanablestringbuilder comme suit:
String firstString = "AAAAAA: ";
String secondString = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
SpannableStringBuilder stringBuilder = new SpannableStringBuilder(firstString + secondString);
stringBuilder.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, firstString.length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
stringBuilder.setSpan(new ForegroundColorSpan(Color.rgb(255, 0, 0)), firstString.length() + 1,
firstString.length() + secondString.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(stringBuilder);
Cela rendra la première phrase audacieuse, et la seconde colorée en rouge.
Autres conseils
Vous pouvez utiliser HTML pour afficher votre texte
myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/first_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAAA: BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" />
</RelativeLayout>
Utilisez-le comme XML.
En Uing TextView unique, vous pourrez le faire. Il vous suffit d'ajouter deux chaînes dans un seul TextView.
textview.setText(first_text+":"+second_text);
Vous avez écrit la position du deuxième texte comme droit du premier texte.
android:layout_toRightOf="@id/first_text"
Donc, selon mes connaissances, il n'est pas possible de continuer en dessous de la première phrase. Il s'agit toujours du deuxième texte uniquement dans le côté droit du premier texte.