显示HTML格式的字符串 [英] Display HTML Formatted String

查看:152
本文介绍了显示HTML格式的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要如何显示我已经打上了简单的HTML到一个TextView的字符串的例子。我发现跨区fromHtml(字符串源),但我不知道如何将它插入我的Java code。

下面是我的Java:

 包com.SorenWinslow.TriumphHistory;

进口android.app.ListActivity;
进口android.os.Bundle;
进口android.widget.ArrayAdapter;

公共类TriumphHistory扩展ListActivity {
    的String [] HistoryList;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        ArrayAdapter<字符串>适配器;
        HistoryList = getResources()getStringArray(R.array.history)。
        适配器=新的ArrayAdapter<字符串> (这一点,R.layout.historylistlayout,HistoryList);
        setListAdapter(适配器);

    }
}
 

下面是历史的一个示例:

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
<字符串数组名=历史>
<项目>< B> 1883年< / B>< BR />有些东西发生了< /项目>
<项目>< B> 1884年< / B>< BR />一些更多的东西发生了< I>前< / I>其他的东西
< /项目>
<资源>
 

下面是我的historylistlayout.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@机器人:ID / text1中
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:textAppearance =机器人:ATTR / textAppearanceLarge
    机器人:重力=center_vertical
    机器人:文字颜色=#FFFFFF
    机器人:后台=#000050
    机器人:layout_marginTop =5像素
    机器人:=了minHeight机器人:ATTR /列表preferredItemHeight
    机器人:填充=的3px
    机器人:TEXTSIZE =8PT机器人:layout_gravity =顶|左/>
 

和这里是我的main.xml

 < XML版本=1.0编码=UTF-8&GT?;


< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:文字颜色=#FFFFFF
    机器人:后台=#000080
    机器人:isScrollContainer =真
    机器人:layout_height =FILL_PARENT
    机器人:layout_width =FILL_PARENT机器人:scrollbarStyle =insideOverlay>

  < ListView控件的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@机器人:ID /列表
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:可点击=真
    机器人:dividerHeight =1像素/>

< / LinearLayout中>
 

解决方案

我试图做一些事情的性质相同,并发现我的HTML以及CSS没有得到正确格式化,所以我把绳子,装成像这样的web视图:

 的WebView的WebView =(web视图)findViewById(R.id.MyWebview);
 字符串摘要=< HTML><身体>您砍下< B> 192< / B>点< / BODY>< / HTML>中;
 webview.loadData(摘要,text / html的,UTF-8);
 

和它认识到所有样式和格式正确无误的HTML。更多从Android参考这里

I need an example of how to display the strings that I have marked up with simple html into a TextView. I have found "Spanned fromHtml(String source)", but I don't know how to plug it into my java code.

Here is my 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);

    }
}

Here is a sample of history:

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

Here is my 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"/>

And here is my 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>

解决方案

I was trying to do something of the same nature and found out that my html along with CSS was not getting formatted correctly so I took the string and loaded it into a webview like this:

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

and it recognized all the styles and formatted the html correctly. More from the android reference HERE

这篇关于显示HTML格式的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆