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

html 修改下划线粗细,TextView设置内容下划线加粗等html样式实例及注意事项TextView设置内容下划线加粗等html样式实例及注意事项效果图test01.pngJava代码packagecom.myapplication;importandroid.app.Activity;importandroid.os.Build;importandroid.os.Bundle;importandroid.text.Html;importandroid.text.Sp…

大家好,又见面了,我是你们的朋友全栈君。

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

效果图

3d2924112d4c?from=timeline

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内的文本:下划线 斜体字 设置字体颜色为红色加粗 “;

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

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”

>

android:textSize=”20sp”

android:id=”@+id/tv1″

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”” />

android:layout_marginTop=”20dp”

android:textSize=”20sp”

android:id=”@+id/tv2″

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”” />

重要的事情说三遍

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143089.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号