项目需求讨论: 文字显示排版— Html格式

嗨,各位,今天来个小技巧,估计不少人都知道,我也就重复提下罢了。。html

好比 android

升级更新框
通知提示框

咱们看到,我用红框框出来的地方 1.直接使用系统自带的AlertDialog的提示框,咱们看到了咱们更新提示里面的具体内容是(-Bug修改 -新增更新提示);而且换行了。bash

2.是自定义的弹框,(自定义弹框用的是我本身封装的类:项目需求讨论-Android 自定义Dialog实现步骤及封装),咱们看到里面的内容会有各类排版,有些是黑色加粗,有些是换行。有些字体可能颜色不一样突出明显,等等需求。ide

归结

归结起来,咱们不多是好几个TextView,而后去本身一个段落一个TextView去呈现,通常都是跟后台约定好,让他传过来HTML格式的字符串字体

因此1.里面咱们就是ui

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(Html.fromHtml(info.getChangeLog()));
复制代码

2.里面咱们是google

TextView message = (TextView) view.findViewById(R.id.message);
if(!TextUtils.isEmpty(content)){
    message.setText(Html.fromHtml(content));
}
复制代码

因此后台传过来的时候,就是多是这种spa

{"content":"<strong><big>低**</big></strong>:本月销售业绩。<br/><br/><strong>诺**</strong>:本月销售业"}
复制代码

而后咱们就一个TextView就能够呈现不一样的格式的内容。只要用Html.fromHtml(String);先转一下,再赋给TextView便可。.net


老司机这就完了?????

答案固然是No。 你会发现Html.fromHtml(String message)这个方法画了横线,已通过时了。WHF。那应该用什么。3d

@SuppressWarnings("deprecation")
public static Spanned fromHtml(String html){
    Spanned result;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
       result = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
    } else {
       result = Html.fromHtml(html);
    }
    return result;
}
复制代码

咱们在Android 6 及如下,仍是使用Html.fromHtml(String);而在Android 7 及以上要用新的:Html.fromHtml(String , flags); 这个Config分为哪些呢:

官方连接走起:Html class documentation

flags有这些:

public static final int FROM_HTML_MODE_COMPACT = 63;
public static final int FROM_HTML_MODE_LEGACY = 0;
public static final int FROM_HTML_OPTION_USE_CSS_COLORS = 256;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 32;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 16;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 2;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 8;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 4;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 1;
public static final int TO_HTML_PARAGRAPH_LINES_CONSECUTIVE = 0;
public static final int TO_HTML_PARAGRAPH_LINES_INDIVIDUAL = 1;
复制代码

看了仍是不懂,不要紧。效果图放出来:

<p style="color: blue;">This is a paragraph with a style</p>

<h4>Heading H4</h4>

<ul>
   <li style="color: yellow;">
      <font color=\'#FF8000\'>li orange element</font>
   </li>
   <li>li #2 element</li>
</ul>

<blockquote>This is a blockquote</blockquote>

Text after blockquote
Text before div

<div>This is a div</div>

Text after div
复制代码

不一样Config的状况下显示的Html格式的文字呈现效果。


继续放招

咱们用以下代码:

String message = "<strong><big>低**</big></strong>:本月销售业绩。<br/><br/><strong>诺**</strong>:本月销售业";

textView.setText(Html.fromHtml(message));
复制代码

OK,没问题。咱们知道会出来咱们上面的自定义提示框的格式。 可是咱们若是是

textView.setText(Html.fromHtml(message)+"");
复制代码

没错,咱们把Html.fromHtml(message)和字符串拼接以后,再传给TextView,那么那些<strong>,<big>等标签就无效了。<br/>仍是有效。

因此咱们若是有需求要拼接字符串,必定要先把要拼接的字符串拼接完后,再用Html.fromHtml包裹,而后赋值给TextView。

有些人说个人字符串不是在代码中,而是在strings.xml中定义,可是直接XML中的<string>标签里面定义不少其余标签:<font>...... 。在低版本的Android设备可能会直接忽略这些标签,这时候咱们可使用<![CDATA[]]>,这个标记所包含的内容将表示为纯文本。好比: <string name="xx"><![CDATA[ 没颜色 <font color=#B0CB4A>有颜色的文字</font> <br/> 换一行显示 <br/> 再换一行显示]]></string>


老司机最后一招

你们都知道,Html格式中改变字体大小的是<font size = "20"></font>可是你会发现:

String htmlText = "<font color=#000000 size=18px>"+productName+"</font> <br/><br/>"
+ "<font color=#ff6600 size=18px>积分:</font>"
+ "<font color=#ff6600 size=20px>+"+productPoint+"分</font>";  

TextView tv = (TextView)findViewById(R.id.productNameAndPoint);
tv.setText(Html.fromHtml(htmlText));
复制代码

咱们的颜色<font color = "#123456">的设置是有效的,可是<font size = "20">却无效。

解决方法: 1.若是项目的字体大小要求不是很精致,只是单纯的为了标题突出等,能够用咱们上面的<big>,<strong>,<small>等 2.咱们自定义标签。思路是替换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<String, String> attributes = new HashMap<String, String>();  
  
    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) {  
        }  
    }  
  
}  
复制代码

还有超连接等其余的HTML标签的实现:

请参考:Html类TagHandler接口


哪里错了但愿你们用力喷我!!! O(∩_∩)O哈哈~

相关文章
相关标签/搜索