TextView:超链接的样式与跳转

xiaoxiao2021-02-28  85

TextView在Android控件中无疑是强大的,能实现的功能很多,这里道长和大家分享一下TextView的用法之一:超链接的样式与跳转。其实用法很简单,道长都不好意思单开一篇博客了

一、基本实现

布局代码 <TextView android:id="@+id/notice_count" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:lineSpacingExtra="9dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="15dp" android:paddingBottom="15dp" android:text="" android:textSize="15sp" /> 实现代码 String message = "单击打开 <a href='http://www.baidu.com/'>百度首页</a>"; notice_count = (TextView)prefectureView.findViewById(R.id.notice_count); notice_count.setText(Html.fromHtml(message)); // 这行代码是必须的 notice_count.setMovementMethod(LinkMovementMethod.getInstance()); 效果图

有的童鞋说这是最简单的实现TextView超链接,如果需求上要求改动超链接的样式怎么办?毕竟原始的样式不怎么样。好,这里道长换一种方式实现

二、另一种实现方式

实现基本链接 ss = new SpannableString("单击打开百度首页"); //创建一个 SpannableString对象 ss.setSpan(new URLSpan("http://www.baidu.com"), 4, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //网络 notice_count.setText(msp); notice_count.setMovementMethod(LinkMovementMethod.getInstance());

然后我们就可以设置TextView的样式了,比如:

设置字体 ss.setSpan(new TypefaceSpan("monospace"), 4, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置字体 设置字体大小 //设置字体大小 // ss.setSpan(new AbsoluteSizeSpan(20), 4, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 第二个参数boolean dp,如果为true,表示前面的字体大小单位为dp,否则为像素。 ss.setSpan(new AbsoluteSizeSpan(20,true), 2, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 设置字体颜色 ss.setSpan(new ForegroundColorSpan(Color.RED), 2, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置字体颜色为红色 设置背景 ss.setSpan(new BackgroundColorSpan(Color.CYAN), 5, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置背景为青色 设置字体样式 // ss.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 1, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 正常 ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 1, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 粗体 // ss.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 2, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 斜体 // ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 2, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 粗斜体 设置下划线 ss.setSpan(new UnderlineSpan(), 2, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置下划线 设置删除线 ss.setSpan(new StrikethroughSpan(), 3, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 设置删除线 设置上下标 ss.setSpan(new SubscriptSpan(), 4, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 下标 ss.setSpan(new SuperscriptSpan(), 6, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 上标

常用的样式大概有这么多。关于超链接道长先说这么多,希望这篇博客为你提供一些帮助。 这里还有道长写的一个工具类,代码如下:

package com.yushan.textviewdemo; import android.graphics.Color; import android.graphics.Typeface; import android.text.SpannableString; import android.text.Spanned; import android.text.style.AbsoluteSizeSpan; import android.text.style.BackgroundColorSpan; import android.text.style.ForegroundColorSpan; import android.text.style.StrikethroughSpan; import android.text.style.StyleSpan; import android.text.style.SubscriptSpan; import android.text.style.SuperscriptSpan; import android.text.style.TypefaceSpan; import android.text.style.URLSpan; import android.text.style.UnderlineSpan; import java.net.URL; /** * Created by beiyong on 2017-5-5. */ public class CustomSpannableString extends SpannableString{ private static SpannableString ss; // 字体 public static final String TYPE_MONOSPACE = "monospace"; public static final String TYPE_DEFAULT = "default"; public static final String TYPE_DEFAULT_BOLD = "default-bold"; public static final String TYPE_SERIF = "serif"; public static final String TYPE_SANS_SERIF = "sans-serif"; // 字体样式 public static final int TYPEFACE_NORMAL = android.graphics.Typeface.NORMAL; public static final int TYPEFACE_BOLD = android.graphics.Typeface.BOLD; public static final int TYPEFACE_ITALIC = android.graphics.Typeface.ITALIC; public static final int TYPEFACE_BOLD_ITALIC = android.graphics.Typeface.BOLD_ITALIC; private static int mStart; private static int mEnd; private CustomSpannableString(CharSequence source){ super(source); } // 创建一个 CustomSpannableString 对象 public static CustomSpannableString getInstance(CharSequence source, int start, int end){ ss = new CustomSpannableString(source); mStart = start; mEnd = end; return (CustomSpannableString)ss; } // 设置网络链接 public CustomSpannableString setTextURL(String url){ ss.setSpan(new URLSpan(url), mStart, mEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return (CustomSpannableString)ss; } // 设置字体 public CustomSpannableString setTextType(String type){ ss.setSpan(new TypefaceSpan(type), mStart, mEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return (CustomSpannableString)ss; } //设置字体大小 public CustomSpannableString setTextSize(int size, Boolean useDp){ if (useDp == null){ ss.setSpan(new AbsoluteSizeSpan(size), mStart, mEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } else { // 第二个参数boolean dp,如果为true,表示前面的字体大小单位为dp,否则为像素。 ss.setSpan(new AbsoluteSizeSpan(size,useDp), mStart, mEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } return (CustomSpannableString)ss; } //设置字体颜色 public CustomSpannableString setTextColor(int color){ ss.setSpan(new ForegroundColorSpan(color), mStart, mEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return (CustomSpannableString)ss; } //设置背景颜色 public CustomSpannableString setTextBackgroundColor(int color){ ss.setSpan(new BackgroundColorSpan(color), mStart, mEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return (CustomSpannableString)ss; } // 设置字体样式 public CustomSpannableString setTextTypeface(int typeface){ ss.setSpan(new StyleSpan(typeface), mStart, mEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return (CustomSpannableString)ss; } // 设置下划线 public CustomSpannableString setTextUnderline(){ ss.setSpan(new UnderlineSpan(), mStart, mEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return (CustomSpannableString)ss; } // 设置删除线 public CustomSpannableString setTextStrikeThrough(){ ss.setSpan(new StrikethroughSpan(), mStart, mEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return (CustomSpannableString)ss; } // 设置上标 public CustomSpannableString setTextSuperscript(int start, int end){ ss.setSpan(new SuperscriptSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return (CustomSpannableString)ss; } // 设置下标 public CustomSpannableString setTextSubscript(int start, int end){ ss.setSpan(new SubscriptSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return (CustomSpannableString)ss; } }

使用方式如下:

private void methodThree(){ css = CustomSpannableString.getInstance("单击打开百度首页", 4, 8) // 创建一个 SpannableString对象 .setTextURL("http://www.baidu.com") .setTextType(CustomSpannableString.TYPE_MONOSPACE) .setTextSize(30, true) .setTextTypeface(CustomSpannableString.TYPEFACE_BOLD) .setTextStrikeThrough() .setTextColor(Color.BLUE) .setTextBackgroundColor(Color.GRAY) .setTextSubscript(4,6) .setTextSuperscript(6,8); notice_count.setText(css); }

效果图如下:

TextView的超链接暂时到这里,希望能给小伙伴们提供一些帮助。大家如果想要Demo看一下,可以自行下载。

源码下载

TextViewLinkDemo


转载请注明原文地址: https://www.6miu.com/read-38051.html

最新回复(0)