更改android

时间:2016-03-12 19:31:49

标签: android html hyperlink

我的目标是在android中将超链接从绿色更改为蓝色。目前整个textview都是绿色的。

enter image description here

我正在使用这个类删除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">&lt;body link=&quot;blue&quot;&gt;By signing in, you agree to our &lt;a href=&quot;com.myapp://http://www.myapp.com/Terms.html&quot;&gt;Terms of Service&lt;/a&gt;
    and &lt;a href=&quot;com.myapp://http://www.myapp.com/Privacy.html&quot;&gt;Privacy Policy&lt;/a&gt;</string>

我在我的java类中设置了它:

    tos.setText(Html.fromHtml(getString(R.string.signing_in)));

如您所见,在我的HTML中,我说:<body link="blue">

包含非超链接词和超链接词的整个TOS是绿色的,如图中所示。为什么超链接不变蓝?

1 个答案:

答案 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"
        ...
        />