【问题标题】:TextView with too long Strings字符串太长的 TextView
【发布时间】:2021-01-20 02:12:41
【问题描述】:

我有这个layout

<ScrollView>
    <TextView android:id="@+id/textView" />
</ScrollView>

当我尝试设置过长的文本时,Textview 不会显示。

//OnCreate
//...
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("..."); //Here is a text with more than 2500
                        //chars and at least have 10 \n char
                        //(it means has at least 10 paragraph)

如何显示该文本?

编辑一个:

即使我将background 设置为TextView 并且TextView 也不会显示该背景

【问题讨论】:

  • 这是您的完整 xml 布局吗?拉取完整代码..
  • 有什么区别? ScrollView 是 xml 父级。两者都有通常的属性来数学/填充 TextView 到 ScrollView
  • 检查你的textView的宽度和高度,高度应该是wrap_content,和父视图一样。如果视图(或其父视图)太小,则不会显示内容
  • 感谢 Ayoub 但不起作用。如果我设置一个大约 500 个字符的字符串,它可以正常工作,但是当使用太长的文本时,就像我使用 setVisibility(View.HIDDEN) 并且什么都没有显示。均匀背景

标签: android android-layout


【解决方案1】:

你可以设置

android:maxLines="1"
android:ellipsize="end"

到你的文本视图,所以比你的文本视图长的文本将被隐藏。

【讨论】:

  • 感谢您的提及。
【解决方案2】:

由于android:singleLine="true" 属性已被弃用,请为您的TextView 设置以下属性:

android:ellipsize="end"
android:maxLines="1"

然后,TextView 将仅显示适合您的 TextView 的文本,后跟省略号 ...

【讨论】:

    【解决方案3】:

    我不知道这是否会对你有所帮助,但在 developer.android 上看到了这个...

    在 Android 8.0(API 级别 26)及更高版本中,您可以指示 TextView 让文本大小自动扩展或收缩,以根据 TextView 的特征和边界填充其布局。此设置可以更轻松地优化具有动态内容的不同屏幕上的文本大小。

    android:autoSizeTextType="uniform"
    

    您可以通过三种方式设置 TextView 的自动调整大小:

    Default

    Granularity

    Preset Sizes

    ================================================ ==

    readMore

    【讨论】:

    • 请在此处添加这些链接中的相关部分。
    • 我不知道这是否对你有帮助,但你有这里的来源:(developer.android.com/guide/topics/ui/look-and-feel/…
    • 为了补充这个答案,代码应该像这样使用。 这可以在上面的文档链接中找到
    【解决方案4】:

    使用这个:

      EditText thumbnailView = (EditText) findViewById(R.id.enterBearingNo_editText_id);
        TextView messageView = (TextView) findViewById(R.id.string2);
        String text = "LargeText";
    
        Display display = getWindowManager().getDefaultDisplay();
        FlowTextHelper.tryFlowText(text, thumbnailView, messageView, display);
    

    FlowTextHelper.class

    class FlowTextHelper {
    
        static boolean mNewClassAvailable;
    
        static {
            if (Integer.valueOf(Build.VERSION.SDK) >= 8) { // Froyo 2.2, API level 8
                mNewClassAvailable = true;
            }
    
            // Also you can use this trick if you don't know the exact version:
            /*
             * try {
             * Class.forName("android.text.style.LeadingMarginSpan$LeadingMarginSpan2"
             * ); mNewClassAvailable = true; } catch (Exception ex) {
             * mNewClassAvailable = false; }
             */
        }
    
        public static void tryFlowText(String text, View thumbnailView,
                TextView messageView, Display display) {
            // There is nothing I can do for older versions, so just return
            if (!mNewClassAvailable)
                return;
    
            // Get height and width of the image and height of the text line
            thumbnailView.measure(display.getWidth(), display.getHeight());
            int height = thumbnailView.getMeasuredHeight();
            int width = thumbnailView.getMeasuredWidth();
            float textLineHeight = messageView.getPaint().getTextSize();
    
            // Set the span according to the number of lines and width of the image
            int lines = (int) Math.round(height / textLineHeight);
            // For an html text you can use this line: SpannableStringBuilder ss =
            // (SpannableStringBuilder)Html.fromHtml(text);
            SpannableString ss = new SpannableString(text);
            ss.setSpan(new MyLeadingMarginSpan2(lines, width), 0, ss.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            messageView.setText(ss);
    
            // Align the text with the image by removing the rule that the text is
            // to the right of the image
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) messageView
                    .getLayoutParams();
            int[] rules = params.getRules();
            rules[RelativeLayout.RIGHT_OF] = 0;
        }
    
        public static void tryFlowTextPrice(String text, TextView messageView,
                Display display) {
            // There is nothing I can do for older versions, so just return
            if (!mNewClassAvailable)
                return;
    
            // Get height and width of the image and height of the text line
            // thumbnailView.measure(display.getWidth(), display.getHeight());
            // int height = thumbnailView.getMeasuredHeight();
            // int width = thumbnailView.getMeasuredWidth();
            float textLineHeight = messageView.getPaint().getTextSize();
    
            // Set the span according to the number of lines and width of the image
            // int lines = (int) Math.round(height / textLineHeight);
            // For an html text you can use this line: SpannableStringBuilder ss =
            // (SpannableStringBuilder)Html.fromHtml(text);
            SpannableString ss = new SpannableString(text);
            // ss.setSpan(new MyLeadingMarginSpan2(lines, width), 0, ss.length(),
            // Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            messageView.setText(ss);
    
            // Align the text with the image by removing the rule that the text is
            // to the right of the image
            // LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
            // messageView
            // .getLayoutParams();
            // int[] rules = params.getRules();
            // rules[RelativeLayout.RIGHT_OF] = 0;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多