如何限制TextView的宽度(并在文本结尾添加三个点)?

我有一个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"/>
272352 次浏览

弃用:

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

更新:

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

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

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

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

你可以在xml中写这一行,其中你取textview:

android:singleLine="true"

我认为你想限制宽度为一行,而不是限制它的字符?由于singleLine已弃用,您可以尝试一起使用以下方法:

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

如。你可以使用

android:maxLength="13"

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

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


} else {


userName = data;


}
textView.setText(userName);

除此之外,你必须使用

 android:maxLines="1"

代码:

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

xml:

android:maxLines = "5"

如。

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

< p >输出: 在马太福音第13章,门徒问耶稣为什么用比喻对众人说话。他回答说:“我告诉你是想让你知道……
你可以限制你的文本视图的字符数,并在文本后添加(…)。 假设你只需要显示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


}

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

<强>编辑: 我已经根据你的有限编号增加了对较少字符的检查。的字符。< / p >

你只要改变…

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>

通过使用,我得到了预期的结果

android:maxLines="2"
android:minLines="2"
android:ellipsize="end"
窍门是将maxLines和minLines设置为相同的值…和不只是android:lines = "2",不做的把戏。

以下是我通过使用各种选项将TextView强制为一行(带和不带三个点)学到的东西。

enter image description here

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 = "结束"

<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 =“招牌”

<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中自动滚动。注意,有时需要在代码中设置:

textView.setSelected(true);

假定android:maxLines="1"android:singleLine="true"应该做基本上相同的事情,由于singleLine是显然弃用,我宁愿不使用它,但当我取出它时,字幕不再滚动。但是,将maxLines取出并不会影响它。

相关:

HorizontalScrollView with scrollhorizontal

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

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

您需要为textview的布局添加以下行

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

希望这对你有用。

在你的课文中加上这两行

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

@AzharShaikh的方法很有效。

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

但我意识到一个麻烦,TextView将被字截断(默认情况下)。显示我们是否有这样的文本:

测试long_line_without_any_space_abcdefgh < em > < / em >

TextView将显示:

< em >测试…< / em >

我找到了解决方案来处理这个问题,用unicode无间断空格字符替换空格,它使TextView包装字符而不是单词:

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

结果:

< em >测试long_line_without_any_space_abc…< / em >

为了使用android: ellipsize属性,你必须限制TextView布局宽度,这样文本就超出了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="这是一个很长的文本要显示"中的文本从带有android: layout_width = " 120 dp”的TextView中消失,android: ellipsize = "结束"将截断文本并将...(3点)放在它之后。即This is very long…将显示在TextView中。

你可以通过xml来实现:

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

我正在使用水平回收视图。
1)在CardView中,TextView在使用

时垂直扭曲
android:ellipsize="end"
android:maxLines="1"

检查粗体的TextViews Wyman Group, Jaskolski… enter image description here < / p >

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

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

检查粗体的TextViews Wyman Group, Jaskolski… enter image description here < / p >

第二种解决方案对我来说很有效(使用singleLine)。我也在操作系统版本:4.1及以上(直到8.0)进行了测试,它工作得很好,没有任何崩溃。

使用

  • 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" />

图片<code>TextView</code>></p></div>
                                                                            </div>
                                </div>
                            </div>
                        </div>
                                                <div class=

        <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"/>

除了

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

你应该设置

android:layout_width="0dp"

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

简单的三点

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

Textview中设置android:maxLength="8"

如果你想设置你的字符串>然后设置textview长度5+3 (3 -dot)

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

1.set static 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" />

添加“…”,如果太长的话,就在文章结尾:

  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" />

通过这种方式,您可以设置视图的最大长度,同时,您将在结尾有圆点("num"是你的dp号吗):

android:maxWidth="{num}dp"
android:ellipsize="end"