編輯:關於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) { } } }
代碼傳送門:喜歡的話,隨手點個star。多謝https://github.com/mcxtzhang/SwipeDelMenuLayout重要的話 開頭說,not for
最近下了個攜程App,點開首頁看,注意到其按鈕在點擊的時候並不是我們經常看到的變色效果,而是先收縮,放開時,再回到原來的大小,感覺這個效果雖然小,但是感覺非常新穎,於是決
代碼中用的自定義常量 public static final int TYPE_Normal = 1; public static final int TYP
ButterKnife是一個專注於Android系統的View注入框架,可以減少大量的findViewById以及 setOnClickListener代碼,可視化一鍵生