我的目标是在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">
包含非超链接词和超链接词的整个TOS是绿色的,如图中所示。为什么超链接不变蓝?
答案 0 :(得分:0)
UrlSpan
只是从强调色中获取链接颜色,因此请为TextView
一种带有蓝色强调色的样式。
styles.xml:
<style name="AppThemeBlueAccent" parent="AppTheme">
<item name="colorAccent">@color/blue</item>
</style>
your_layout.xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/AppThemeBlueAccent"
...
/>