9

単純な html でマークアップした文字列を TextView に表示する方法の例が必要です。"Spanned fromHtml(String source)" を見つけましたが、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);

    }
}

履歴のサンプルを次に示します。

<?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>

ここに私の 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"/>

そして、ここに私の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>
4

5 に答える 5

13

私は同じ性質の何かをしようとしていたのですが、HTML と CSS が正しくフォーマットされていないことがわかったので、文字列を取得して、次のように Web ビューにロードしました。

 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");

すべてのスタイルを認識し、html を正しくフォーマットしました。Androidリファレンスの詳細はこちら

于 2011-02-01T20:24:41.523 に答える
12

前述の関数を使用してスパンを返し、返されたスパンオブジェクトのtoStringを使用してテキストビューのテキストを次のように設定し、フォーマットされたテキストを作成します。

Spanned marked_up = Html.fromHtml(yourTextWithHTML);
((TextView) findViewById(R.id.text1)).setText(marked_up.toString());

これは、yourTextWithHTMLが投稿したアイテムタグ内の任意の文字列である場合に機能します。例えば"<b>1883</b><br/>Some stuff happened"

于 2010-03-19T16:50:53.510 に答える
8
    Spanned spannedContent = Html.fromHtml(htmlString);
    textView.setText(spannedContent, BufferType.SPANNABLE);
于 2010-10-16T00:44:22.850 に答える
1

これは実際には最も簡単なようです (文字列リソースの HTML から? ):

mTextView.setText(getText(R.string.my_styled_text));

これは、内部に HTML を含む文字列に対して機能します。

于 2010-11-09T02:15:21.010 に答える
0

私は同じ問題に直面し、それを使用して解決しました

    CharSequence[] HistoryList = getResources().getTextArray(R.array.history);

上記のコードは、リソースに関連付けられたスタイル付きテキスト配列を返します。CharSequence[] を返します

次に、要素を使用します

   setText(HistoryList[index]);
于 2013-02-28T09:01:53.360 に答える