【问题标题】:Html.fromHtml() on TextView not working with layouts added dynamicallyTextView 上的 Html.fromHtml() 不适用于动态添加的布局
【发布时间】:2017-04-11 23:27:00
【问题描述】:

我正在使用Html.fromHtml() 函数在多个屏幕中设置 TextView 的文本。它们都具有以下模式:<a href="http://link.com">Name</a>。我使用日志来查看字符串是否正确,并且它们是正确的。

但是在这两种情况下,有一个共同点是不起作用的:我正在膨胀和复制行布局。

我将 LinearLayout 用作一行并将其中的多个添加到另一个线性布局中。

正在复制的行布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="3">

    <TextView
        android:id="@+id/bandName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:layout_marginTop="15dp"
        android:text="Band"
        android:textColor="@color/darkTxtLightBg"
        android:textColorLink="@color/darkTxtLightBg"
        android:clickable="true"
        android:linksClickable="true"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/bandOnTour"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginTop="15dp"
        android:gravity="end"
        android:text="Band"
        android:textColor="@color/darkTxtLightBg"
        android:textSize="16sp" />
</LinearLayout>

我在其中插入行的 LinearLayout:

<LinearLayout
        android:id="@+id/table_bandList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical"
        android:layout_marginTop="10dp">

</LinearLayout>

我复制、填充和添加这些视图的代码:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout tr;
for (Band b : bandsFollowed) {
     tr = (LinearLayout) inflater.inflate(R.layout.bandrow_followedlist, null);
     TextView t1 = (TextView) tr.findViewById(R.id.bandName);
     t1.setText(Html.fromHtml("<a href=\"" + b.url + "\">" + b.name + "</a>"));
     t1 = (TextView) tr.findViewById(R.id.bandOnTour);
     t1.setText(getResources().getString(R.string.onTour));
     t1.setTextColor(getResources().getColor(R.color.bandFollowed));
     tl.addView(tr);
}

我尝试了什么:

  • 将所有父布局设置为android:clickable="true",但没有任何区别。
  • android:onClick 属性与函数一起使用,但这不会像我需要的那样工作。
  • 使用 TableLayout 和 TableRows 而不是 LinearLayouts,但我遇到了同样的问题。

那么,为什么当我简单地将文本设置到我拥有的另一个 TextView 时它会起作用,但当我动态添加它们时它不起作用?会不会是充气器、父级甚至 xml 文件的问题?

我知道链接在那里,因为文本带有下划线,但是当我单击它时没有任何反应。

【问题讨论】:

  • 请阅读my answer here 看看它是否适合您
  • @BNK 没用,但感谢您的帮助。就像链接在那里一样,文本带有一些链接下划线,但是当我单击时没有任何反应。

标签: android android-layout


【解决方案1】:

试试下面的代码,我已经测试过了。希望这会有所帮助!

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        final String htmlString="<a href=\"http://www.google.com\">Go to Google</a>";
        textView.setText(Html.fromHtml(htmlString));

【讨论】:

  • 就是这样!谢谢!我之前在另一项活动中使用过该行,但我忘记在另一项上添加setMovementMethod...我的错误。
猜你喜欢
  • 2011-07-22
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 2013-09-01
  • 2013-08-07
  • 1970-01-01
相关资源
最近更新 更多