【问题标题】:Android, How to limit width of TextView (and add three dots at the end of text)?Android,如何限制 TextView 的宽度(并在文本末尾添加三个点)?
【发布时间】:2012-05-31 16:54:27
【问题描述】:

我有一个TextView,我想限制它的字符。实际上,我可以做到这一点,但我正在寻找的是如何在字符串末尾添加三个点 (...)。这表明文本一直在继续。这是我的 XML,但没有点,虽然它限制了我的文本。

<TextView 
        android:id                      = "@+id/tvFixture"
        android:layout_width            = "wrap_content"
        android:layout_height           = "wrap_content"
        android:layout_toLeftOf         = "@id/ivFixture_Guest"
        android:text                    = "@string/test_06"
        android:lines                   = "1"
        android:ems                     = "3"
        android:gravity                 = "right"
        style                           = "@style/simpletopic.black" 
        android:ellipsize="end"/>

【问题讨论】:

    标签: android textview ellipsis


    【解决方案1】:

    已弃用:

    在您的 Textview 中再添加一个属性 android:singleLine="true"

    更新:

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

    【讨论】:

    • 我刚尝试这个时肯定需要 singleLine。即使它说已弃用。
    • android:singleLine="true" 已被弃用并且有不良的副作用。改用 android:ellipsize="end" 和 maxLine="1"
    • 使用singleLine 的“不良副作用”是什么?另见:Is the xml attribute singleLine deprecated or not in Android?
    • 顺便说一句,如果你愿意,android:maxLines 可以大于 1 :)
    • 有人收到这个消息“ W/StaticLayout: maxLineHeight 不应该是-1。maxLines:1 lineCount:1” >?
    【解决方案2】:

    以下是我通过尝试将TextView 强制为单行(有和没有三个点)的各种选项而学到的。

    android:maxLines="1"

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text="one two three four five six seven eight nine ten" />
    

    这只是将文本强制为一行。任何额外的文本都被隐藏。

    相关:

    ellipsize="end"

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="one two three four five six seven eight nine ten" />
    

    这会切断不适合的文本,但通过添加省略号(三个点)让用户知道该文本已被截断。

    相关:

    ellipsize="marquee"

    <TextView
        android:id="@+id/MarqueeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="one two three four five six seven eight nine ten" />
    

    这会使文本在 TextView 中自动滚动。注意sometimes需要在代码中设置:

    textView.setSelected(true);
    

    据说android:maxLines="1"android:singleLine="true" 应该做基本相同的事情,因为singleLine 是apparently deprecated 我宁愿不使用它,但是当我把它拿出来时,选框不再滚动了。不过,将maxLines 取出并不会影响它。

    相关:

    Horizo​​ntalScrollView with scrollHorizo​​ntally

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/horizontalScrollView">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:scrollHorizontally="true"
            android:text="one two three four five six seven eight nine ten" />
    </HorizontalScrollView>
    

    这允许用户手动滚动查看整行文本。

    【讨论】:

    • 但这只是在每个单词的末尾省略。有没有办法在very_long_word_without_whitespace...中间添加...?
    • @TobiasReich,我不知道有内置的方法来实现这一点。您可以使用Paint.measureText 自己制作。
    • 有时你可能还需要 app:layout_constrainedWidth="true"
    【解决方案3】:

    在你的布局文件中试试 TextView 的这个属性..

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

    【讨论】:

      【解决方案4】:

      我认为您想将宽度限制为一行而不是按字符限制?由于singleLine 已被弃用,您可以尝试同时使用以下内容:

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

      【讨论】:

        【解决方案5】:

        例如。你可以使用

        android:maxLength="13"
        

        这会将 texview 长度限制为 13,但问题是如果您尝试添加 3 个点(...),它不会显示它,因为它将是 textview 长度的一部分。

             String userName;
             if (data.length() >= 13) {
                    userName = data.substring(0, 13)+ "...";
        
             } else {
        
                    userName = data;
        
            }
                textView.setText(userName);
        

        除此之外你必须使用

         android:maxLines="1"
        

        【讨论】:

        • 您最好使用“\u2026”而不是“...”
        • 这只是一个例子。如果我将字符串放在 Strings.xml 中,那么我肯定会选择 unicode。有什么具体的原因要放在这里吗?
        • 不。我个人最近了解到这个字符在 Unicode 中的存在,并决定分享这些知识;)
        【解决方案6】:

        使用

        • android:singleLine="true"
        • android:maxLines="1"
        • app:layout_constrainedWidth="true"

        这就是我完整的TextView 的样子:

            <TextView
            android:id="@+id/message_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:maxLines="1"
            android:singleLine="true"
            android:text="NAME PLACEHOLDER MORE Text"
            android:textColor="@android:color/black"
            android:textSize="16sp"
        
            android:textStyle="bold"
            app:layout_constrainedWidth="true"
            app:layout_constraintEnd_toStartOf="@id/message_check_sign"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintStart_toEndOf="@id/img_chat_contact"
            app:layout_constraintTop_toTopOf="@id/img_chat_contact" />
        

        【讨论】:

          【解决方案7】:

          我正在使用 Horizo​​​​ntal Recyclerview。
          1) 在 CardView 中,TextView 在使用时会垂直扭曲

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

          检查粗体的 TextViews Wyman Group, Jaskolski...

          2) 但是当我使用 singleLine 和 ellipsize -

          android:ellipsize="end"
          android:singleLine="true"
          

          检查粗体的 TextViews Wyman Group, Jaskolski...

          第二个解决方案对我很有效(使用 singleLine)。我还测试了操作系统版本:4.1 及更高版本(直到 8.0),它工作正常,没有任何崩溃。

          【讨论】:

            【解决方案8】:

            代码:

            TextView your_text_view = (TextView) findViewById(R.id.your_id_textview);
            your_text_view.setEllipsize(TextUtils.TruncateAt.END);
            

            xml:

            android:maxLines = "5"
            

            例如

            在马太福音第 13 章,门徒问耶稣为什么用比喻对群众说话。他回答说:“天国的奥秘是赐给你们的,但没有赐给他们。

            输出: 在马太福音第 13 章,门徒问耶稣为什么用比喻对群众说话。他回答说:“已经让你知道了……

            【讨论】:

              【解决方案9】:

              我通过使用得到了想要的结果

              android:maxLines="2"
              android:minLines="2"
              android:ellipsize="end"
              

              诀窍是将 maxLines 和 minLines 设置为相同的值...而不仅仅是 android:lines = "2",不要这样做。 此外,您正在避免任何已弃用的属性。

              【讨论】:

                【解决方案10】:

                您可以限制文本视图的字符数并在文本后添加 (...)。 假设您只需要显示 5 个字母,然后您需要显示 (...),只需执行以下操作:

                String YourString = "abcdefghijk";
                
                if(YourString.length()>5){
                
                YourString  =  YourString.substring(0,4)+"...";
                
                your_text_view.setText(YourString);
                }else{
                
                 your_text_view.setText(YourString); //Dont do any change
                
                }
                

                一个小技巧^_^。虽然它不是一个好的解决方案。但是一项对我有用的工作:D

                编辑: 根据您的有限编号,我已添加检查较少字符。字符数。

                【讨论】:

                【解决方案11】:

                您需要在 textview 的布局中添加以下几行

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

                希望这对你有用。

                【讨论】:

                  【解决方案12】:

                  为了使用 android:ellipsize 属性,您必须限制 TextView布局宽度,这样 text 超出了 TextView 的范围。

                  所以,android:layout_width 属性在这里起着关键作用,相应地设置它。

                  一个例子可以是:

                  <TextView        
                      android:layout_width="120dp"
                      android:layout_height="wrap_content"
                      android:ellipsize="end"
                      android:text="This is a very long text to be displayed"
                      android:textSize="12sp"
                      android:maxLines="1"            
                       />
                  

                  现在,如果 android:text="This is a very long text to be displayed" 中的 text 在 TextView 中消失,并带有 android:layout_width="120dp", android:ellipsize="end" 将截断 文本 并放置 ...(3 个点) 在它之后。即这很长……会显示在TextView中。

                  【讨论】:

                    【解决方案13】:
                            <TextView
                                android:id="@+id/product_description"
                                android:layout_width="165dp"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="2dp"
                                android:paddingLeft="12dp"
                                android:paddingRight="12dp"
                                android:text="Pack of 4 summer printed pajama"
                                android:textColor="#d2131c"
                                android:textSize="12sp"
                                android:maxLines="2"
                                android:ellipsize="end"/>
                    

                    【讨论】:

                      【解决方案14】:

                      我认为你给出了文本视图的固定高度和宽度。那么您的解决方案将起作用。

                      【讨论】:

                        【解决方案15】:

                        你可以在 xml 中写下这一行,你可以在其中使用 textview

                        android:singleLine="true"
                        

                        【讨论】:

                          【解决方案16】:

                          @AzharShaikh 的方法效果很好。

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

                          但我意识到 TextView 会被单词截断(默认情况下)的问题。显示我们是否有如下文本:

                          测试 long_line_without_any_space_abcdefgh

                          TextView 将显示:

                          测试...

                          我找到了解决这个问题的解决方案,用 unicode 不间断空格字符替换空格,它使 TextView 在字符而不是单词上换行:

                          yourString.replace(" ", "\u00A0");
                          

                          结果:

                          测试 long_line_without_any_space_abc...

                          【讨论】:

                            【解决方案17】:

                            简单的三个点

                            android:layout_width="100dp" <!--your dp or match_parent or 0dp>
                            android:maxLines="2" <!--count your line>
                            android:ellipsize="end"
                            

                            【讨论】:

                              【解决方案18】:

                              如果文本太长,在文本末尾添加“...”的步骤:

                              1. 检查文本宽度是否恒定
                              2. 添加这两行 android:ellipsize="end" android:maxLines="1"

                              约束布局中textview的完整代码:

                              <TextView
                                      android:layout_width="75dp"
                                      android:layout_height="wrap_content"
                                      android:ellipsize="end"
                                      android:maxLines="1"
                                      android:textSize="15sp"
                                      android:textAllCaps="false"
                                      android:textStyle="bold"
                                      app:layout_constraintBottom_toBottomOf="parent"
                                      app:layout_constraintEnd_toEndOf="parent"
                                      app:layout_constraintStart_toStartOf="parent"
                                      app:layout_constraintTop_toBottomOf="parent" />
                              

                              【讨论】:

                              • 您好,感谢您的回答。多个其他答案,例如 this onethis one 已经解释了你所说的一切,所以当你有足够的声誉时,你将能够投票而不是重复
                              【解决方案19】:

                              在文本中添加这两行

                              android:ellipsize="end"
                              android:singleLine="true"
                              

                              【讨论】:

                                【解决方案20】:

                                除了

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

                                你应该设置

                                android:layout_width="0dp"
                                

                                也称为“匹配约束”,因为wrap_content 值只是将框扩大到适合整个文本,而ellipsize 属性无法发挥作用。

                                【讨论】:

                                  【解决方案21】:

                                  你可以通过xml

                                  <TextView 
                                      android:id="@+id/textview"
                                      android:maxLines="1" // or any number of lines you want
                                      android:ellipsize="end"
                                      />
                                  

                                  【讨论】:

                                  • 我怎样才能把 3 Dots @ 结尾与 maxLength="35" 属性任何解决方案?
                                  【解决方案22】:

                                  Textview中设置android:maxLength="8"

                                  如果你想设置 yourString >5。然后设置 textview 长度 5+3(对于三点)

                                  if (yourString.length()>5) // 
                                      {
                                          textview.setText(yourString.substring(0,5)+"...");
                                      }
                                      else {
                                          textview.setText(title);
                                      }
                                  

                                  【讨论】:

                                    【解决方案23】:

                                    1.设置静态width like 7odp

                                    2.使用android:ellipsize="end"

                                    3.使用android:maxLines="1"

                                    4.使用android:singleLine="true"

                                    <TextView
                                            android:id="@+id/tv_status"
                                            **android:layout_width="70dp"**
                                            android:layout_height="wrap_content"
                                            android:layout_marginEnd="@dimen/padding_8"
                                            android:gravity="center|center_horizontal"
                                            android:includeFontPadding="false"
                                            android:textColor="@color/black_2a"
                                            android:textSize="@dimen/text_size_1"
                                            **android:ellipsize="end"
                                            android:maxLines="1"
                                            android:singleLine="true"**
                                            app:layout_constrainedWidth="true"
                                            app:borrowStatusText="@{item.lenders[0].status}"
                                            app:layout_constraintEnd_toStartOf="@id/iv_vector"
                                            app:layout_constraintTop_toTopOf="parent" />
                                    

                                    【讨论】:

                                      【解决方案24】:

                                      你只是改变......

                                      android:layout_width="wrap_content"
                                      

                                      使用下面的行

                                      android:layout_width="match_parent"
                                      

                                      .......

                                         <LinearLayout
                                             android:layout_width="wrap_content"
                                             android:layout_height="wrap_content"
                                             android:layout_centerVertical="true"
                                             android:layout_marginLeft="10dp"
                                             android:layout_marginTop="10dp"
                                             android:layout_toRightOf="@+id/visitBox"
                                             android:orientation="vertical" >
                                      
                                                   <TextView
                                                        android:id="@+id/txvrequestTitle"
                                                        android:layout_width="match_parent"
                                                        android:layout_height="wrap_content"
                                                        android:singleLine="true"
                                                        android:text="Abcdefghighiklmnon"
                                                        android:textAppearance="? 
                                                        android:attr/textAppearanceLarge"
                                                        android:textColor="@color/orange_color" />
                                      
                                         </LinearLayout>
                                      

                                      【讨论】:

                                      • singleLine 已弃用
                                      猜你喜欢
                                      • 2020-11-17
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 2019-12-22
                                      • 1970-01-01
                                      • 1970-01-01
                                      相关资源
                                      最近更新 更多