[英]Aligning text in Textview for Android (using Html.fromhtml())
[英]Set text in TextView using Html.fromHtml
我必須分三個部分顯示文本,所以我使用了Html.fromHtml,如下所示:
txtvw.setText(Html.fromHtml("<p align=right> <b> "
+ "Hi!" + " </br> <font size=6>"
+ " How are you "+"</font> </br>"
+ "I am fine" + " </b> </p>"));
HTML是正確的,但在設備中它顯示在一行。
我的textview xml聲明是:
<RelativeLayout
android:id="@+id/Home"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="@drawable/transparentfooter"
android:layout_above="@+id/bottombar" >
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="@android:color/white"/>
</RelativeLayout>
您使用<br>
標簽的方式不合適。 使用以下內容:
txtvw.setText(Html.fromHtml("<p align=right> <b> "
+ "Hi!" + " <br/> <font size=6>"
+ " How are you "+"</font> <br/>"
+ "I am fine" + " </b> </p>"));
它應該是<br/>
而不是</br>
我已經測試了這個代碼並且它按預期顯示了3行。
Html.fromHtml(String source)
現在從Api-24貶值。
從Api-24這個方法改為
Html.fromHtml(String source,int flags)
所以我們可以使用下面的Api 24
txtvw.setText(Html.fromHtml("<p align=right> <b> "
+ "Hi!" + " <br/> <font size=6>"
+ " How are you "+"</font> <br/>"
+ "I am fine" + " </b> </p>"),Html.FROM_HTML_MODE_LEGACY);
將行標記設置為你的布局android:lines =“4”
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:lines="4"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>
寫出正確的“br”html標簽
TextView text =(TextView)findViewById(R.id.text);
text .setText(Html.fromHtml("<p align=right> <b> "
+ "<br>" +"Hi!" + " </br> "
+ "<br> How are you "+" </br>"
+ "<br>I am fine" + " </br> </b> </p>"));
textView.setText(Html.fromHtml(str,Html.FROM_HTML_MODE_COMPACT));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.