【问题标题】:Hyperlink is not accepting space in android超链接不接受android中的空间
【发布时间】:2013-09-15 02:47:55
【问题描述】:

它可能看起来很简单,但我很困在这个问题上,我想在我的应用程序中添加一个超链接例如:www.example of an address with spaces.html

但是当我将 url 放入代码中时,它看起来像这样

这是我的代码

<TextView
        android:id="@+id/link"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:linksClickable="true"

        android:autoLink="web"
        android:layout_marginLeft="10dp"
        android:textColor="@color/white"
        android:text="http://www.example of an address with spaces.html" />

【问题讨论】:

  • 不要在 URL 中使用空格。他们不属于那里。但是:在字符串中使用 %20(而不是空格),那么它应该可以工作。
  • 我知道,但有没有其他方法可以做到这一点

标签: android html xml hyperlink


【解决方案1】:

试试下面的代码

tvTermsOfUse.setText(Html.fromHtml(getString(R.string.tv_terms_of_use_html)));
    Linkify.addLinks(tvTermsOfUse, Linkify.ALL);
    tvTermsOfUse.setMovementMethod(LinkMovementMethod.getInstance())

 <TextView
    android:id="@+id/tv_terms_of_use"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:textAlignment="gravity"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:textSize="15sp"
    />


<string name="tv_terms_of_use_html"><![CDATA[This is link to <a href="http://google.com/">Google with spaces</a>.]]></string>

【讨论】:

    【解决方案2】:

    如何做到这一点programmatically简单易行

    从您的 xml code 中删除 android:text="http://www.example of an address with spaces.html"

    将关注添加到您的java code

    TextView txt = (TextView) findViewById(R.id.link);
    txt.setText(Html.fromHtml("<a href=www.google.com>link with space</a>"));
    

    【讨论】:

      【解决方案3】:

      试试下面的,

      TextView mTextView1 = (TextView) findViewById(R.id.my_text_view_html);
      String text =
      "Visit <a href=\"http://www.google.com/\">Google home page</a>";
      mTextView1.setText(Html.fromHtml(text));
      mTextView1.setMovementMethod(LinkMovementMethod.getInstance());
      

      【讨论】:

        【解决方案4】:

        试试 XML CDATA

         <string name="demoStr"><Data><![CDATA[ <b>ABC</b> ]]> </Data></string>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-13
          • 2014-06-17
          • 1970-01-01
          • 2020-12-31
          • 1970-01-01
          • 2016-04-03
          • 1970-01-01
          • 2016-12-13
          相关资源
          最近更新 更多