Sor*_*ren 9 java eclipse android android-arrayadapter
我需要一个示例,说明如何使用简单的html将标记的字符串显示到TextView中.我找到了"跨越fromHtml(字符串源代码)",但我不知道如何将其插入到我的java代码中.
这是我的Java:
package com.SorenWinslow.TriumphHistory;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class TriumphHistory extends ListActivity {
String[] HistoryList;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter;
HistoryList = getResources().getStringArray(R.array.history);
adapter = new ArrayAdapter<String> (this,R.layout.historylistlayout,HistoryList);
setListAdapter(adapter);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是历史样本:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="history">
<item><b>1883</b><br/>Some stuff happened</item>
<item><b>1884</b><br/>Some more stuff happened <i>before</i> the other stuff
</item>
<resources>
Run Code Online (Sandbox Code Playgroud)
这是我的historylistlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:textColor="#ffffff"
android:background="#000050"
android:layout_marginTop="5px"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="3px"
android:textSize="8pt" android:layout_gravity="top|left"/>
Run Code Online (Sandbox Code Playgroud)
这是我的main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:textColor="#ffffff"
android:background="#000080"
android:isScrollContainer="true"
android:layout_height="fill_parent"
android:layout_width="fill_parent" android:scrollbarStyle="insideOverlay">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:dividerHeight="1px"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
nin*_*nse 13
我试图做一些相同的性质,发现我的HTML和CSS没有正确格式化,所以我把字符串加载到这样的webview:
WebView webview = (WebView) findViewById(R.id.MyWebview);
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", "utf-8");
Run Code Online (Sandbox Code Playgroud)
它识别所有样式并正确格式化html.更多来自android引用HERE
jqp*_*liq 12
使用您提到的函数返回spanned,然后使用返回的Spanned对象的toString设置textview的文本,以便实现格式化文本:
Spanned marked_up = Html.fromHtml(yourTextWithHTML);
((TextView) findViewById(R.id.text1)).setText(marked_up.toString());
Run Code Online (Sandbox Code Playgroud)
这将适用于yourTextWithHTML是您发布的项目标签中的任何字符串.例如"<b>1883</b><br/>Some stuff happened"
Spanned spannedContent = Html.fromHtml(htmlString);
textView.setText(spannedContent, BufferType.SPANNABLE);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36383 次 |
| 最近记录: |