Android:TextView

时间:2017-05-19 11:39:05

标签: android

我有一个TextView,其中包含一些包含链接的html文本。 我在TextView中添加了一个样式,现在该链接不作为链接显示。

字符串:

<string name="strPrivacyPolicyAndContact">See our %1$s Privacy Policy %2$s or contact us for more information: support@beatrixkiddo.com.</string>

TextView xml:

<TextView
    android:id="@+id/privacyAndContact"
    style="@style/NewFont_Label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:gravity="left"
    android:text="@string/strPrivacyPolicyAndContact" />

NewFont_Label样式:

<style name="NewFont_Label">
    <item name="android:textSize">12sp</item>
    <item name="variant">medium</item>
    <item name="android:textColor">@color/label</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:letterSpacing">0.02</item>
</style>

代码:

private static final String PRIVACY_HREF = "<a href=\"http://www.beatrixkiddo.com\privacy\">";
private static final String END_TAG = "</a>";

privacyAndContact = (TextView) view.findViewById(R.id.privacyAndContact);
privacyAndContact.setText(Html.fromHtml(getString(R.string.strPrivacyPolicyAndContact, PRIVACY_HREF, END_TAG)));

在添加样式之前:

enter image description here

添加样式后:

enter image description here

问题:

我已经尝试添加所有可能的html标签但没有结果 如何使用下划线或颜色设置链接的样式?

更新

我已经尝试过SpannableString,但是在TextView上应用style会影响它的影响:

SpannableString privacy = new SpannableString(getString(R.string.strPrivacyPolicyAndContact));
privacy.setSpan(new URLSpan("http://www.beatrixkiddo.com/privacy"), 13, 26, 0);
privacy.setSpan(new UnderlineSpan(), 13, 26, 0);
privacy.setSpan(new ForegroundColorSpan(Color.RED), 13, 26, 0);
privacyAndContact.setText(privacy);

因此,在这种情况下,spannable也不起作用。

1 个答案:

答案 0 :(得分:1)

仅改变这两行。

private static final String PRIVACY_HREF = "<u><b><font color='green' ><a href='http://www.beatrixkiddo.com/privacy' >Privacy Policy";
private static final String END_TAG = "</a></font></b></u>";
textView.setText(Html.fromHtml(PRIVACY_HREF+END_TAG)); 
相关问题