【发布时间】:2016-06-28 00:05:06
【问题描述】:
我的目标是在 android 中将超链接从绿色更改为蓝色。目前整个textview都是绿色的。
我正在使用这个类来删除 android 中超链接的下划线 - 我在 SO 上找到了它:
public class TextViewNoUnderline extends AppCompatTextView {
public TextViewNoUnderline(Context context) {
this(context, null);
}
public TextViewNoUnderline(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}
public TextViewNoUnderline(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setSpannableFactory(Factory.getInstance());
}
private static class Factory extends Spannable.Factory {
private final static Factory sInstance = new Factory();
public static Factory getInstance() {
return sInstance;
}
@Override
public Spannable newSpannable(CharSequence source) {
return new SpannableNoUnderline(source);
}
}
private static class SpannableNoUnderline extends SpannableString {
public SpannableNoUnderline(CharSequence source) {
super(source);
}
@Override
public void setSpan(Object what, int start, int end, int flags) {
if (what instanceof URLSpan) {
what = new UrlSpanNoUnderline((URLSpan) what);
}
super.setSpan(what, start, end, flags);
}
}
}
然后在我的 xml 中我这样做了:
<com.myapp.customshapes.TextViewNoUnderline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tos"
tools:text="@string/signing_in"/>
signing_in 是这个字符串的位置:
<string name="signing_in"><body link="blue">By signing in, you agree to our <a href="com.myapp://http://www.myapp.com/Terms.html">Terms of Service</a>
and <a href="com.myapp://http://www.myapp.com/Privacy.html">Privacy Policy</a></string>
我已经在我的 java 类中设置了这个:
tos.setText(Html.fromHtml(getString(R.string.signing_in)));
如你所见,在我的 html 中,我说:<body link="blue">
包括非超链接词和超链接词的整个服务条款是绿色的,如图所示。为什么超链接没有变成蓝色?
【问题讨论】:
-
您可以使用 spannablestringbuilder 代替 html 文本
-
你能举个例子吗,如果可行,我会接受你的回答
-
stackoverflow.com/questions/7570239/… 在 stackoveflow 上有很多类似的。我之前可能回答过类似的问题,但不记得链接了