我想在中心修复一些字符串文本。我怎样才能做到这一点? 例如,看看这张照片
我有一个这样的字符串。现在我希望我的字符串的一些单词(标题)在水平中心的TextView
中显示,但其他单词将保持原样。我怎么能这样做?
我尝试了以下代码,但它不适用于各种维度的设备。
String string= " SYRIA
\n\nSyria oh Syria why do you bleed?
\nBrother fights brother without thought or need
\nRuled by a tyrant for so many years
\nAnd now the split blood is washed away by tears\n"
答案 0 :(得分:0)
您可以使用Html.fromHtml
。使用此功能,您只需要单textview
。要做到这一点,它应该完美地运作
your_textview.setText(Html.fromHtml("<h><center><b><u>SYRIA</u></b></center></h>\n" +
" <p>Syria oh Syria why do you bleed?</p> </br>\n" +
" <p>Brother fights brother without thought or need</p> </br>\n" +
" <p>Ruled by a tyrant for so many years</p> </br>\n" +
" <p>And now the split blood is washed away by tears</p> </br>"));
答案 1 :(得分:0)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:text="seria"
android:textSize="20sp"
/>
<TextView
android:id="@+id/textView2"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:text="Syria oh Syria why do you bleed?
\nBrother fights brother without thought or need
\nRuled by a tyrant for so many years
\nAnd now the spilt blood is washed away by tears"
/>
</LinearLayout>
答案 2 :(得分:0)
您可以使用2个不同的Textview,或者如果有单个textview,您可以使用HTML或Spannable String。 Spannable可以如下使用:
TextView resultView = new TextView(this);
final String SyriaText = "Syria";
final String OtherText = "Other Text";
final String result = SyriaText + " " + OtherText;
final SpannableString finalResult = new SpannableString(result);
finalResult.setSpan((new AlignmentSpan.Standard(Alignment.ALIGN_CENTER), 0, SyriaText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
resultView.setText(finalResult);
您也可以使用Spannable进行样式设置。您可以从以下网址了解详情:https://developer.android.com/reference/android/text/Spannable.html