1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android代码设置maxlength android-使用maxLength使用3个点结束TextView

android代码设置maxlength android-使用maxLength使用3个点结束TextView

时间:2024-01-28 21:33:31

相关推荐

android代码设置maxlength android-使用maxLength使用3个点结束TextView

android-使用maxLength使用3个点结束TextView

我的布局中有一个TextView,它是layout_width中的wrap_content。 最多限制为15个字符,因此我正在使用maxLength。

我需要以3个点(...)结束此TextView,并且仅当我使用dp给layout_width提供固定大小时才会发生,这是我不想做的事情。

我知道可以通过代码做到这一点,并在第15个字符后剪切字符串,然后添加3个点,但是我更喜欢通过XML来实现。

知道如何以3个点结束文本并保留wrap_content吗?

android:id="@+id/inbox_contactName"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:lines="1"

android:maxLines="1"

android:ellipsize="end"

android:singleLine="true"

android:maxLength="15"

android:textColor="#0670b4"

android:textSize="16sp" />

Yaniv asked -02-04T07:39:38Z

10个解决方案

68 votes

这将解决您的问题,在XML代码中使用singleLine属性

android:ellipsize="end"

android:maxEms="15"

android:singleLine="true"

编辑:singleLine已弃用。 请改用maxlines="1"。

Kirk answered -02-04T07:39:58Z

17 votes

我从评论中收集到@Yaniv已经使用代码解决了它-但这是正确的方法(使用xml)。 可能会帮助登陆这里的其他用户。 技巧是同时使用toleftof和torightof。

...

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:singleLine="true"

android:layout_toRightOf="@id/some_element1"

android:layout_toLeftOf="@id/some_element2"/>

...

Ravi answered -02-04T07:40:18Z

11 votes

尽管可以定义Maximum EMS,但不能同时使用maxLength和Ellipsize,请参见下面的示例

android:id="@+id/tv_hist_source_lang"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:ellipsize="end"

android:maxEms="8"

android:maxLines="1"

android:text="TextView"

android:textAppearance="?android:attr/textAppearanceMedium" />

AZ_ answered -02-04T07:40:38Z

10 votes

您可以使用

android:maxWidth="100dp"

android:maxLines="1"

android:ellipsize="end"

只有这对我有用。

Zhou Hongbo answered -02-04T07:41:02Z

5 votes

使用这个android:ellipsize="end"

Mahmoud Hashim answered -02-04T07:41:22Z

4 votes

最简单的方法之一是添加Right Padding + Ellipsize

android:id="@+id/txtvw_contentcell_subhead"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Polem sampler, Lorem Ipsum golep tolem burop yemit noski"

android:ellipsize="end"

android:maxLines="1"

android:textColor="@color/caption_black_color"

android:textSize="@dimen/caption_size"

android:paddingRight="40dp"/>

这是带有字符数限制的示例字幕文本。

Enzokie answered -02-04T07:41:46Z

0 votes

我需要使用单选按钮执行此操作,并且我想将大小限制为一行。 当我使用Android:singleline="true"时,单选按钮的选中圆圈消失了。 这是最终起作用的代码:

android:ellipsize="end"

android:maxLines="1"

无需设置em。 这在TextView和其他UI组件中也可能起作用。 希望这对某人有帮助。

Micer answered -02-04T07:42:11Z

0 votes

您可以修改长度大于20的String,以添加椭圆尺寸。

Horatio answered -02-04T07:42:31Z

0 votes

非常重要:ellipsize = "end"仅在maxLength设置为大于一行字符数的值时才有效。如果您将TextView的末端对齐并开始到任何其他视图,则可以使用wrap_content。

Benjamin Livinus answered -02-04T07:42:52Z

-1 votes

从xml删除行

android:lines="1"

android:maxLines="1"

Muhammad Aamir Ali answered -02-04T07:43:12Z

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。