美文网首页
TextView设置内容下划线加粗等html样式实例及注意事项

TextView设置内容下划线加粗等html样式实例及注意事项

作者: 温兴婷 | 来源:发表于2018-01-26 15:53 被阅读0次

TextView设置内容下划线加粗等html样式实例及注意事项

效果图

test01.png

Java代码

package com.myapplication;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv1 = (TextView)findViewById(R.id.tv1);
        TextView tv2 = (TextView)findViewById(R.id.tv2);
        String st = "TextView内的文本:<u>下划线</u> <i>斜体字</i> <font color='#ff0000'>设置字体颜色为红色</font><strong>加粗</strong> ";
        Spanned  result ;
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.N){//androidN+之后废除了Html.fromHtml(String),用Html.fromHtml(String,flag)代替
            result= Html.fromHtml(st,Html.FROM_HTML_MODE_LEGACY);
        }else {
            result =Html.fromHtml(st);
        }
        //必须直接写result
        tv1.setText(result);
        //如果拼接其他内容则无效,比如:
        tv2.setText("添加其他内容无效"+result);

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="left"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
   >
    <TextView
        android:textSize="20sp"
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
    <TextView
        android:layout_marginTop="20dp"
        android:textSize="20sp"
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
</LinearLayout>

重要的事情说三遍

  • 不要再settext中添加其他内容,直接放入Html.fromHtml()方法得到的返回值,不然无效样式无效
  • 不要再settext中添加其他内容,直接放入Html.fromHtml()方法得到的返回值,不然无效样式无效
  • 不要再settext中添加其他内容,直接放入Html.fromHtml()方法得到的返回值,不然无效样式无效

相关文章

  • TextView设置内容下划线加粗等html样式实例及注意事项

    TextView设置内容下划线加粗等html样式实例及注意事项 效果图 Java代码 activity_main....

  • Android 常用小知识

    TextView 添加下划线 Webview 加载html ListView,RecyclerView等设置具有p...

  • Android Html设置TextView的颜色、加粗样式

    01、概述 在开发需求中,会遇到字符串中的某个部分变色,加粗的需求。当然可是使用SpannableString 来...

  • 04_Android TextView 相关

    TextView 添加下划线 设置 TextView 的文本内容预览,但在项目运行中不显示 3.设置 Textvi...

  • Android 控件各种属性

    Android控件各种属性设置 TextView设置中间或者底部横线 TextView设置字体加粗

  • Android TextView部分文字实现点击事件

    一个TextView设置部分内容的点击事件及样式,Android的TextView中早已为开发人员提供好了这样的A...

  • 内容操作

    html()--设置/读取标签内部的html内容 获取box样式类里面的html内容 .html(); 设置box...

  • TextView、EditText属性简介

    字体粗体设置: TextView的其他不常用属性: 给textView加下划线方法: 字体其他设置方法: 设置输入...

  • CSS基础

    css全称为“层叠样式表”,它主要用于定义HTML内容在浏览器内显示样式,如文字大小、颜色、字体加粗等。css有四...

  • 4-css概述

    css全称为层叠样式表,它主要是用于定义HTML内容在浏览器内的显示样式,如文字大小,颜色,字体加粗等。 css代...

网友评论

      本文标题:TextView设置内容下划线加粗等html样式实例及注意事项

      本文链接:https://www.haomeiwen.com/subject/astwaxtx.html