【问题标题】:Limit the Length of TextView [duplicate]限制 TextView 的长度 [重复]
【发布时间】:2012-10-18 16:40:33
【问题描述】:

可能重复:
Limit text length of EditText in Android

我是 Activity 上的 textView,它根据从 JSON 响应中接收到的参数显示,我只需要将其限制为 12 个字符。

<TextView
    android:id="@+id/textViewName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/includeheadersetting"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:gravity="center"
    android:text="Name"
    android:textColor="#000000"
    android:textStyle="bold" />

【问题讨论】:

    标签: android


    【解决方案1】:

    添加类似代码

    android:maxLength="12"
    

    【讨论】:

      【解决方案2】:

      android:maxLength="12" 添加到您的文本视图中..

      <TextView
                  android:id="@+id/textViewName"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@+id/includeheadersetting"
                  android:layout_marginLeft="20dp"
                  android:layout_marginTop="20dp"
                  android:gravity="center"
                  android:text="Name"
                  android:textColor="#000000"
                  android:textStyle="bold"
                  android:maxLength="12" />
      

      【讨论】:

        【解决方案3】:

        使用android:maxLength="12"限制文字长度

         <TextView
                    android:id="@+id/textViewName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/includeheadersetting"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="20dp"
                    android:gravity="center"
                    android:text="Name"
                    android:maxLength="12"
                    android:textColor="#000000"
                    android:textStyle="bold" />
        

        您还可以使用其他属性,如下所示:

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

        使用此属性“...”将添加到文本的末尾,如下所示:

        “你好……” 而不是 “你好,你好吗?”

        【讨论】:

        • 第 1 行:“你好,你好吗”第 2 行:“你”代替你好,你好吗
        • android:singleLine="true" 已弃用。您必须改用 android:maxLines="1"
        【解决方案4】:

        一般来说,只包括android:maxLength 不是好主意。

        使用 maxLength 属性,然后使用android:ellipsize="marquee" 自动将“...”添加到任何已被截断的行的末尾。

        <TextView 
            android:id="@+id/txtView" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:maxLines="1" 
            android:maxLength="10" 
            android:ellipsize="marquee"/>
        

        【讨论】:

        【解决方案5】:

        将以下最大长度参数添加到您的文本视图中-

        android:maxLength="12"
        

        无论你想要什么限制,你都可以替换它,而不是 12 可以给出 14 或你想要的任何长度。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-03-28
          • 2012-02-05
          • 1970-01-01
          • 2011-04-19
          • 1970-01-01
          • 1970-01-01
          • 2011-09-21
          相关资源
          最近更新 更多