Textview中字符串太大调试时候手机上只显示STRING_TOO_LARGE

Textview中做一个类似京东的隐私政策所以要用到很大的字符串在as工具上正常显示文字 手机调试的时候就不能正常显示只显示STRING_TOO_LARGE

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;
}

 

第一步:将隐私政策的 xxx.txt 文本放在assets下;

第二步:读取assets下的 xxx.txt 文件:

/**
 * 从assets下的txt文件中读取数据
 */
public String initAssets(String fileName) {
    String str = null;
    try {
        InputStream inputStream = getAssets().open(fileName);

        str = getString(inputStream);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return str;
}
public static String getString(InputStream inputStream) {
    InputStreamReader inputStreamReader = null;
    try {
        inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }
    BufferedReader reader = new BufferedReader(inputStreamReader);
    StringBuffer sb = new StringBuffer("");
    String line;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line);
            sb.append("\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}

 

第三步: 显示 xxx.txt 内容到控件上:

String str = initAssets("xxx.txt");

tv_content.setText(str);

 

截个图 或者(设置一下字体根据手机分辨率大小自动调整)

开发的时候,把长宽字体用属性纪录下来,resize的时候根据长宽比例调整

https://stackoverflow.com/questions/51732252/textview-shows-string-too-large

设置

android:singleLine="true"

android:ellipsize="end"

或者

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

textview自动调整大小

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

  <TextView
      android:layout_width="match_parent"
      android:layout_height="200dp"
      app:autoSizeTextType="uniform"
      app:autoSizeMinTextSize="12sp"
      app:autoSizeMaxTextSize="100sp"
      app:autoSizeStepGranularity="2sp" />

</LinearLayout>

或者8.0以上

 

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:autoSizeTextType="uniform"
    android:autoSizeMinTextSize="12sp"
    android:autoSizeMaxTextSize="100sp"
    android:autoSizeStepGranularity="2sp" />

建议使用 TextView 加载Html 的形式,自行添加 html 标签

干啥一定要用TextView,用WebView加载静态页面多好,省的自己写布局。