Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android textview html font標簽不好用

android textview html font標簽不好用

編輯:關於Android編程

textview可以顯示html標簽的效果,但是最基本的字體大小,顏色font標簽卻不好用,根本無法使用設置字體大小,看了下源碼原來是個bug,在設置font屬性的時候就沒有設置字體大小,考慮html還提供自定標簽的功能,思路是替換font標簽自己解析設置。

用到的接口是Html類TagHandler接口

public class DdbFontHandler implements TagHandler {

	private int startIndex = 0;
	private int stopIndex = 0;

	@Override
	public void handleTag(boolean opening, String tag, Editable output,
			XMLReader xmlReader) {
		processAttributes(xmlReader);
		
		if(tag.equalsIgnoreCase(ddbfont)){
			if(opening){
				startFont(tag, output, xmlReader);
			}else{
				endFont(tag, output, xmlReader);
			}
		}

	}
	
    public void startFont(String tag, Editable output, XMLReader xmlReader) {  
        startIndex = output.length();  
    }  
  
    public void endFont(String tag, Editable output, XMLReader xmlReader){  
        stopIndex = output.length();  
        
        String color = attributes.get(color);
        String size = attributes.get(size);
        size = size.split(px)[0];
        if(!TextUtils.isEmpty(color) && !TextUtils.isEmpty(size)){
	        output.setSpan(new ForegroundColorSpan(Color.parseColor(color)), startIndex, stopIndex,  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
	        output.setSpan(new AbsoluteSizeSpan(Utils.dipToPx(GApp.instance(), Integer.parseInt(size))), startIndex, stopIndex,  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
        }else{
        	output.setSpan(new ForegroundColorSpan(0xff2b2b2b), startIndex, stopIndex,  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  
        }
    } 
    
    final HashMap attributes = new HashMap();

    private void processAttributes(final XMLReader xmlReader) {
        try {
            Field elementField = xmlReader.getClass().getDeclaredField(theNewElement);
            elementField.setAccessible(true);
            Object element = elementField.get(xmlReader);
            Field attsField = element.getClass().getDeclaredField(theAtts);
            attsField.setAccessible(true);
            Object atts = attsField.get(element);
            Field dataField = atts.getClass().getDeclaredField(data);
            dataField.setAccessible(true);
            String[] data = (String[])dataField.get(atts);
            Field lengthField = atts.getClass().getDeclaredField(length);
            lengthField.setAccessible(true);
            int len = (Integer)lengthField.get(atts);

            /**
             * MSH: Look for supported attributes and add to hash map.
             * This is as tight as things can get :)
             * The data index is just where the keys and values are stored. 
             */
            for(int i = 0; i < len; i++)
                attributes.put(data[i * 5 + 1], data[i * 5 + 4]);
        }
        catch (Exception e) {
        }
    }

}
這個比較通用,自定義其他標簽。網上還有針對這個問題其他思路,也是不錯的

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved