【问题标题】:Changing color of hyperlink in android在android中更改超链接的颜色
【发布时间】: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">&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 中,我说:&lt;body link="blue"&gt;

包括非超链接词和超链接词的整个服务条款是绿色的,如图所示。为什么超链接没有变成蓝色?

【问题讨论】:

  • 您可以使用 spannablestringbuilder 代替 html 文本
  • 你能举个例子吗,如果可行,我会接受你的回答
  • stackoverflow.com/questions/7570239/… 在 stackoveflow 上有很多类似的。我之前可能回答过类似的问题,但不记得链接了

标签: android html hyperlink


【解决方案1】:

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

【讨论】:

  • 感谢工作 - 我必须使用 android:theme,例如:android:theme="@style/BlueAccent"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 1970-01-01
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多