TextView显示STRING_TOO_LARGE

6

最近我一直在尝试将一个超过25k字节的字符串设置到TextView中,但一直收到“STRING_TOO_LARGE”错误输出。

有人说在运行时设置字符串可能会解决这个问题,于是我修改了代码,但似乎没有什么作用。我也尝试直接在setText()中设置资源ID,但同样也没有任何作用。

我在Activity中使用以下方法来显示terms_dialog

private void showTermsPopup() {
    Dialog termsDialog = new Dialog(this);
    termsDialog.setContentView(R.layout.terms_dialog);
    termsDialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT);

    // Cancel the dialog if you touch the background
    termsDialog.setCanceledOnTouchOutside(true);

    // Add the terms string to the dialog
    TextView termsText = termsDialog.findViewById(R.id.termsTextView);
    String terms = getResources().getString(R.string.terms);
    termsText.setText(terms);

    // Show the dialog
    termsDialog.show();
}

This is the terms_dialog.xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:padding="8dp"
        android:scrollbarSize="0dp">

        <TextView
            android:id="@+id/termsTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>

</android.support.constraint.ConstraintLayout>

就是我得到的结果(敏感信息已涂白)。

2
请在此处查看 - https://issuetracker.google.com/issues/110853846 - HB.
@HB。所以我应该将字符串添加到文本文件中,然后从那里读取吗? - nikolouzos
3个回答

14

正如@HB. 给我的链接所指出的那样,在您的 APK 文件中,您不能有超过32,767字节(以UTF-8编码)的字符串。

所以,我做的是在assets文件夹中创建了一个名为terms.txt的txt文件,然后将字符串放入其中。接下来,使用以下函数将该文件转换为字符串:

private String getTermsString() {
    StringBuilder termsString = new StringBuilder();
    BufferedReader reader;
    try {
        reader = new BufferedReader(
                new InputStreamReader(getAssets().open("terms.txt")));

        String str;
        while ((str = reader.readLine()) != null) {
            termsString.append(str);
        }

        reader.close();
        return termsString.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

然后我将它分配给了TextView


我有同样的问题,但我不认为以前有这个问题。 - dan dan

0
在这种情况下,您可以像下面的代码一样使用2个TextView,并将您的文本(项目或隐私政策)分成两部分加载到两个TextView中。
<ScrollView
    android:id="@+id/SCROLLER_ID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/_20sdp"
    android:fillViewport="true"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/TEXTVIEW_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:linksClickable="true"
            android:text="FIRST_HALF_TEXT"
            android:textColor="@color/color_text_color"
            android:textColorLink="@color/ic_blue_gray_500"
            android:textSize="@dimen/_12ssp" />

        <TextView
            android:id="@+id/TEXTVIEW_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:linksClickable="true"
            android:visibility="gone"
            android:text="SECOND_HALF_TEXT"
            android:textColor="@color/color_text_color"
            android:textColorLink="@color/ic_blue_gray_500"
            android:textSize="@dimen/_12ssp" />
    </LinearLayout>
</ScrollView>

-3

使用android:ellipsize来适应文本

        <TextView
        android:id="@+id/termsTextView"
        android:ellipsize="middle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

敬礼!


1
我必须显示整个字符串,因为这些是应用程序的条款和条件。 - nikolouzos
1
你可以将字符串分成两半并放入两个TextView中,这是一种“简短而基础”的方法。或者你可以将它放在图片或PDF中。 - DGLeiva

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接