目錄 搜尋
android Manifest Manifest.permission Manifest.permission_group android.accessibilityservice AccessibilityService android.accounts Account android.app NotificationManager android.bluetooth BluetoothAdapter BluetoothClass BluetoothClass.Device BluetoothClass.Device.Major BluetoothClass.Service BluetoothDevice BluetoothServerSocket BluetoothSocket android.content SharedPreferences android.database.sqlite SQLiteCursorDriver SQLiteOpenHelper android.graphics Bitmap android.location LocationListener Geocoder GpsStatus GpsStatus.Listener GpsStatus.NmeaListener GpsSatellite android.media AudioFormat AsyncPlayer AudioRecord AudioRecord.OnRecordPositionUpdateListener ThumbnailUtils AudioManager android.net TrafficStats MailTo LocalSocket android.os AsyncTask AsyncTask.Status CountDownTimer Message MessageQueue HandlerThread android.text Html android.util JsonWriter android.view ContextMenu ContextMenu.ContextMenuInfo Display ViewManager View ViewStub ViewTreeObserver ViewParent WindowManager GestureDetector Gravity MenuInflater ScaleGestureDetector SoundEffectConstants android.view.inputmethod InputConnection InputMethod InputMethodSession BaseInputConnection InputMethodManager android.widget AbsListView AbsListView.LayoutParams AbsListView.OnScrollListener AbsListView.RecyclerListener AbsoluteLayout AbsoluteLayout.LayoutParams AbsSeekBar AbsSpinner AdapterView AdapterView.AdapterContextMenuInfo AdapterView.OnItemLongClickListener AdapterView.OnItemSelectedListener AdapterView.OnItemClickListener AnalogClock BaseAdapter BaseExpandableListAdapter Button CheckBox CheckedTextView Checkable Chronometer Chronometer.OnChronometerTickListener CompoundButton CompoundButton.OnCheckedChangeListener CursorAdapter CursorTreeAdapter DatePicker DatePicker.OnDateChangedListener DialerFilter DigitalClock EditText Filter Filter.FilterListener Filter.FilterResults ExpandableListAdapter ExpandableListView.OnChildClickListener ExpandableListView.OnGroupClickListener ExpandableListView.OnGroupCollapseListener ExpandableListView.OnGroupExpandListener Filterable Gallery Gallery.LayoutParams GridView GridLayout GridLayout.Alignment RadioGroup ImageView ImageView.ScaleType HorizontalScrollView ImageButton ImageSwitcher FilterQueryProvider ListAdapter ListView MediaController MultiAutoCompleteTextView MultiAutoCompleteTextView.CommaTokenizer MultiAutoCompleteTextView.Tokenizer QuickContactBadge RadioButton RatingBar RatingBar.OnRatingBarChangeListener RelativeLayout RemoteViews ResourceCursorAdapter ResourceCursorTreeAdapter Scroller ScrollView SearchView SearchView.OnCloseListener SearchView.OnQueryTextListener SearchView.OnSuggestionListener SeekBar SeekBar.OnSeekBarChangeListener SimpleAdapter SimpleAdapter.ViewBinder SimpleCursorAdapter SimpleCursorAdapter.CursorToStringConverter SimpleCursorAdapter.ViewBinder SimpleCursorTreeAdapter SimpleCursorTreeAdapter.ViewBinder SimpleExpandableListAdapter SlidingDrawer SlidingDrawer.OnDrawerCloseListener SlidingDrawer.OnDrawerOpenListener SlidingDrawer.OnDrawerScrollListener Spinner SpinnerAdapter WrapperListAdapter TabHost TabHost.TabSpec TextView TimePicker TimePicker.OnTimeChangedListener Toast TableLayout TableLayout.LayoutParams TableRow TableRow.LayoutParams TabWidget TextSwitcher ToggleButton TwoLineListItem VideoView ViewAnimator ViewFlipper ViewSwitcher ViewSwitcher.ViewFactory ZoomButtonsController ZoomButtonsController.OnZoomListener ZoomButton ZoomControls dalvik.system DexFile
文字


Html

版本:Android 3.2

 

结构

继承关系

public class Html extends Object

        

java.lang.Object

         android.text.Html

 

类概述

该类主要用来格式化html格式的文本,像浏览器一样对html标签进行解析,渲染输出,但并不是所有的标签都被支持!(译者注:当然你自定义的标签默认肯定不被支持,但可以通过实现Html.TagHandler这个接口来自我解析。下面会讲到。)

 

内部类

interface     Html.ImageGetter

用来检索HTML中的<img>标签。(译者注:如果实现了这个接口,当解析的时候遇到<img> 标签时会回调ImageGettergetDrawable(String source)方法,并返回一个Drawable对象,该方法参数为<img>标签的src属性的值)

 

interface     Html.TagHandler

用来通知当解析器遇到无法识别的标签时该作出何种处理

 

 

公共方法

public static Spanned fromHtml (String source)

返回通过TagSoup TagSoup 是一个Java开发符合SAXHTML解析器 授权协议)解析器解析之后的可直接显示的文本,任何<img>则由默认的一般图片所替代,当然也可以通过实现Html.ImageGetter接口来显示真正的图片。

参数

source      待处理的html文本

 

         public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)

同上,不同的是当遇到img标签,或解析器无法识别的标签时分别触发注册的两个对象来进行处理。

参数

source               为待处理的html文本

imageGetter    Html.ImageGetter对象

tagHandler       Html.TagHandler

 

public static String toHtml (Spanned text)

该方法完全可以理解为fromHtml()的可逆函数,将Spanned 类型的文本还原为HTML文本。

 

补充

         文章精选

                   android开发:TextView中显示HTML和图片

                   android TextView HTML富文本应用

         示例代码

public class Main extends Activity {

 

   @Override

   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);

      TextView tv = (TextView) findViewById(R.id.demo);

     

      // 貌似都不支持HTML标签的属性,及CSS样式,下面使用到的字体颜色都不被支持

      //String html = "<h2>html</h2><pstyle='color:red;'>这是测试内容</p><p><img src='http://www.baidu.com/img/baidu_sylogo1.gif'></p>";

String html = "<h2>html测试</h2><p font='red'>这是测试内容</p><p><img src='http://www.baidu.com/img/baidu_sylogo1.gif'></p>";

     

      tv.setText(Html.fromHtml(html,new Html.ImageGetter(){

 

        @Override

        public Drawable getDrawable(String source) {

           Drawable myDrawable = null;

           try {

              myDrawable = Drawable.createFromStream(new URL(source).openStream(), "baidu_sylogo1.gif");

           } catch (MalformedURLException e) {

              e.printStackTrace();

           } catch (IOException e) {

              e.printStackTrace();

           }

          

           myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight());

          

           return myDrawable;

        }},null));

   }

 

}

 

 

 


上一篇: 下一篇: