解释
我在里面使用了 textview 有一个链接,当我点击它时它会转到网页。为了创建链接,我使用了 html 的 anchor 标记。
这是我创建链接的xml文件
<TextView
android:text="@string/Home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill_horizontal"
android:layout_weight="0.5"
android:clickable="true"
android:textColorLink="#b3b3b3"
android:textSize="15dp"
android:id="@+id/Terms" />
这是我创建链接的 string.xml 文件
<string name="Home"><a href="https://www.google.co.in/ " style="color:rgb(179, 179, 179);text-decoration:none">Home</a>
我尝试使用 css 样式的 text-decoration:none 不起作用。
我也尝试从这个链接 Remove underline from links in TextView - Android 在那个 spannable 在 c# 中不起作用
问题
如何去掉超链接的下划线?
最佳答案
How to remove underline from the hyperlink textview in xamarin android
您上面发布的链接是正确的。我简化了 Reuben Scratton
的代码并且能够删除下划线。
URLSpanNoUnderline
类:
public class URLSpanNoUnderline : URLSpan
{
public URLSpanNoUnderline(String url) : base(url)
{
}
public override void UpdateDrawState(TextPaint ds)
{
base.UpdateDrawState(ds);
ds.UnderlineText = false;
}
}
从超链接 TextView
中删除下划线:
private void stripUnderlines(TextView textView)
{
SpannableString s = new SpannableString(textView.Text);
s.SetSpan(new URLSpanNoUnderline(textView.Text), 0, s.Length(), SpanTypes.ExclusiveExclusive);
textView.SetText(s, TextView.BufferType.Spannable);
}
更新:
这是解决点击问题的解决方法:
您可以使用 ClickableSpan
而不是 URLSpan
来实现相同的功能。可以引用my answer ,然后像这样修改您的代码:
textview.MovementMethod = LinkMovementMethod.Instance;
textview.Text = GetString(Resource.String.Home);
SpannableString ss = new SpannableString(textview.Text);
ss.SetSpan(new MyClickableSpan(this), 0, ss.Length(), SpanTypes.ExclusiveExclusive);
textview.SetText(ss, TextView.BufferType.Spannable);
这是 MyClickableSpan
代码:
class MyClickableSpan : ClickableSpan
{
private MainActivity mainActivity;
public MyClickableSpan(MainActivity mainActivity)
{
this.mainActivity = mainActivity;
}
public override void OnClick(View widget)
{
Intent browserIntent = new Intent(Intent.ActionView, Uri.Parse("https://www.google.co.in/"));
mainActivity.StartActivity(browserIntent);
}
public override void UpdateDrawState(TextPaint ds)
{
base.UpdateDrawState(ds);
ds.Color = Color.Red;
ds.UnderlineText = false;
}
}
关于c# - 如何从 xamarin android 中的超链接 TextView 中删除下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48238579/