在android中更改超链接的颜色
我的目标是在android中将超链接从绿色更改为蓝色。目前,整个文本视图是绿色的 我正在使用这个类删除android中超链接的下划线-我发现它是这样的:在android中更改超链接的颜色,android,html,hyperlink,Android,Html,Hyperlink,我的目标是在android中将超链接从绿色更改为蓝色。目前,整个文本视图是绿色的 我正在使用这个类删除android中超链接的下划线-我发现它是这样的: public class TextViewNoUnderline extends AppCompatTextView { public TextViewNoUnderline(Context context) { this(context, null); } public TextViewNoUnde
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"/>
如您所见,在我的html中,我说:
整个TOS(包括非超链接词和超链接词)如图中所示为绿色。为什么超链接没有变成蓝色 UrlSpan
UrlSpan
仅从强调色中获取链接颜色,因此为您的TextView
提供一种带有蓝色强调色的样式
styles.xml:
<style name="AppThemeBlueAccent" parent="AppTheme">
<item name="colorAccent">@color/blue</item>
</style>
@颜色/蓝色
您的_layout.xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/AppThemeBlueAccent"
...
/>
您可以使用spannablestringbuilder而不是html文本。请给我一个例子,如果它有效,我会接受您的答案,因为stackoveflow上有许多类似的答案。我以前可能回答过类似的问题,但我记不起起起作用的链接感谢-我不得不使用android:theme
,例如:android:theme=“@style/BlueAccent”
<style name="AppThemeBlueAccent" parent="AppTheme">
<item name="colorAccent">@color/blue</item>
</style>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/AppThemeBlueAccent"
...
/>