【问题标题】:TextView shows STRING_TOO_LARGETextView 显示 STRING_TOO_LARGE
【发布时间】:2018-08-07 17:21:23
【问题描述】:

一段时间以来,我一直在尝试将一个大字符串(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();
}

这是terms_dialog.xml 文件:

<?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>

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

【问题讨论】:

标签: java android


【解决方案1】:

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

所以,我所做的是在名为 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

【讨论】:

  • 我有同样的问题,但我认为我以前没有遇到过问题。
【解决方案2】:

在这种情况下,您可以像下面的代码一样采用 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" />
    

    问候!

    【讨论】:

    • 我必须显示整个字符串,因为这些是应用程序的条款和条件。
    • 可以把String切成两半,放到两个TextView中。这是一种“简短而基本”的方式。或者您可以将其放入图像或 PDF 中。
    猜你喜欢
    • 2019-05-29
    • 2013-04-26
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    相关资源
    最近更新 更多