c# - 如何从 xamarin android 中的超链接 TextView 中删除下划线

标签 c# xamarin.android

解释

我在里面使用了 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# 中不起作用

问题

如何去掉超链接的下划线?

This is the image showing the hyperlink with underline

最佳答案

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/

相关文章:

c# - 在 C# 中用常量替换硬编码字符串

c# - 如何从 Xamarin 客户端连接到 SignalR 服务器?

android - MonoDroid 数据存储

c# - 无法将 Signalr Client 库作为可移植库添加到 Xamarin 项目

xamarin - 离开 Xamarin.Forms 应用程序中的页面后,Webview 不关闭

xamarin - 在Xamarin Forms中如何调用MainActivity的方法或从另一个类获取MainActivity的 'Window'?

c# - SQL 字符串或二进制数据将被截断,列名

c# - 它不会返回类中的值

c# - emguCV-快速计算二进制图像中带孔的对象的面积

c# - ASP.NET C# + Nhibernate HTTP 模块 (Session per request) - 限制请求类型