【问题标题】:Android Linkify links textColor ignored, css style overrides possible?Android Linkify 链接 textColor 被忽略,css 样式覆盖可能吗?
【发布时间】:2011-08-18 17:19:00
【问题描述】:

我在我的应用程序中使用Linkify,并且访问的链接文本显示为深紫色。我的整体布局背景颜色是深蓝色,因此无法阅读。文本设置为白色,但访问过的链接显示为深紫色。如何覆盖它?

<TextView android:text="Website:" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"
          android:textSize="14dip"
          android:paddingBottom="2dip"
          android:background="#ffffff"
          android:textColor="#577dbe" />              
<TextView android:text="http://www.mysite.com/"
          android:layout_width="match_parent" 
          android:layout_height="wrap_content"
          android:textSize="12dip"
          android:paddingBottom="6dip"
          android:textColor="#ffffff"
          android:id="@+id/contactWeb1" />  

【问题讨论】:

    标签: android textview overriding linkify textcolor


    【解决方案1】:

    原来是一个简单的解决方案

    但是,您将无法进行 visited / not visited 区分。

        TextView contactWeb1 = (TextView) findViewById(R.id.contactWeb1);
        noteView.setText("http://www.blablaasd.com/");
        noteView.setLinkTextColor(Color.red); //for example
        Linkify.addLinks(noteView, Linkify.ALL);
    

    我的尝试捕捉访问过的状态

    使用

        noteView.setLinkTextColor(getResources().getColorStateList(R.color.colors));
    

    而不是

        noteView.setLinkTextColor(Color.red);
    

    res/ 中创建文件夹color 并在res/color/ 中创建colors.xml

    colors.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item
          android:state_window_focused="true" android:color="#00ff00">
            
        </item>
        <item
          android:state_window_focused="true" android:color="#00ff00">
            
        </item>
        <item android:color="#FF00ff"/>
    </selector>
    

    我已尽力捕捉访问过的州。我尝试了选择器可以采用的所有状态。

    我可能错过了如果你发现了,分享(:


    替代解决方案(仅适用于 html 链接)

    以编程方式设置字体颜色

    缺点(注意这一点)

    • 你必须知道它是否被访问过(这是可行的)

      这意味着您没有覆盖 已访问链接 功能。

    代码

    TextView contactWeb1 = (TextView) findViewById(R.id.contactWeb1);
    String desc = "<font color=\"red\"><a href='http://www.mysite.com/'>Visit my site</a></font>";
    contactWeb1.setText(Html.fromHtml(desc));
    contactWeb1.setMovementMethod(LinkMovementMethod.getInstance());
    

    【讨论】:

    • 嘿,我不会检查链接是否被访问 :) 我只是使用 linkify 使可点击的对象可点击,比如地址(自动转到谷歌地图)电话号码和网络链接,有点像您如何在 android 上的电子邮件中单击这些内容,或者如何单击网页上的电话号码,它们不是 html 链接,但它们仍然是可点击的!所以这可能是解决方案!
    • 您的代码仅检查 HTML 链接,这对于 linkify 应该执行的电话号码和地址将如何工作。即使之后使用phone1.setTextColor(Color.parseColor("#ffffff")); 设置颜色也不起作用并被忽略
    • 嗨,我有一个问题,regd 相同,我们可以将 css 文件与 html 标签集成在一起
    • @Aada AFAIK 你不能使用带有Html.fromHtml的css文件
    【解决方案2】:

    添加:

    android:textColor="#ffffff"
    

    到 xml 中的 TextView 元素解决了问题...似乎覆盖 textcolor 会覆盖与元素相关的其他颜色样式

    看到这个问题:

    Android text view color doesn't change when disabled

    【讨论】:

    • textcolor 已在我提供的代码中看到,请使用原始答案重试
    【解决方案3】:

    我尝试了您的代码,但我的 TextView 的颜色没有改变。那么一个解决方案是向 TextView 添加一个 onClick 侦听器并在其中设置 TextView 的颜色。因此,每当单击文本时,它将被设置为您指定的颜色。

    【讨论】:

    • 这并不能解决textview被访问后颜色错误的问题
    【解决方案4】:

    我在使用 Linkify 时遇到了同样的问题。您可以改用LinkMovementMethod,并将您的文本转换为SpannableString

    TextView contactWeb1 = (TextView) findViewById(R.id.contactWeb1);
    SpannableString str = SpannableString.valueOf(contactWeb1);
    str.setSpan(new URLSpan(contactWeb1.getText()), 0, str.length() -1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    contactWeb1.setMovementMethod(LinkMovementMethod.getInstance());
    

    【讨论】:

      猜你喜欢
      • 2018-08-04
      • 2016-05-07
      • 2013-09-01
      • 2013-05-06
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      相关资源
      最近更新 更多