android删除字符串最后,Android:TextView自动截断并替换最后3个字符串的字符串(Android: TextView automatically truncate and repla...

Android:TextView自动截断并替换最后3个字符串的字符串(Android: TextView automatically truncate and replace last 3 char of String)

如果一个String比TextView的宽度长,它会自动包装到下一行。 我可以通过使用android:singleLine (弃用)或设置android:inputType="text"来避免这种情况。 我现在需要的是用“ ... ”取代String的最后3个字符。 由于我不使用等宽字体,这将永远不同,取决于我的String使用的字母。 所以我想知道在TextView获取String的最后3个字符的最佳方式是什么,并替换它们。 也许Android框架中已经有了一些实现,因为这是一个常见的问题。

If a String is longer than the TextView's width it automatically wraps onto the next line. I can avoid this by using android:singleLine (deprecated) or by setting android:inputType="text". What I need now is something that replaces the last 3 characters of my String with "...". Since I'm not using a monospace font this will always be different depending on the letters used in my String. So I'm wondering what's the best way to get the last 3 characters of a String in a TextView and replace them. Maybe there's already something implemented in the Android framework, since this must be a common problem.

原文:https://stackoverflow.com/questions/1666736

更新时间:2019-11-22 07:11

最满意答案

您应该可以使用文本视图的“ellipsize”属性:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/text_mytext"

android:ellipsize="end"

android:singleLine="true"

/>

您也可能需要对布局应用重力值; 有时我看到没有他们的“自动伸展”的意见。

You should be able to use the "ellipsize" property of a text view:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/text_mytext"

android:ellipsize="end"

android:maxLines="1"

/>

You may also need to apply gravity values to the layout too; I have sometimes seen "auto-stretching" views without them.

2017-10-18

相关问答

$text = 'the quick brown fox jumps over the lazy dog';

$textLength = strlen($text);

$maxChars = 16;

$result = substr_replace($text, '...', $maxChars/2, $textLength-$maxChars);

$结果现在是: the quic...lazy dog

$text = 'the quick brown fox jumps over the

...

如果是单行尝试这个: android:ellipsize="end"

android:maxLines="1"

android:scrollHorizontally="true"

android:singleLine="true"

它为我工作 See my update 3. The workaround was using " marquee". I have seen many apps doing that at this time. Now, with new versions of A

...

您应该可以使用文本视图的“ellipsize”属性:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/text_mytext"

android:ellipsize="end"

android:singleLine="true"

/>

您也可能需要对布局应用重力值; 有时我看到没有他们的“自动伸展”的意见。

...

确保你阅读并理解了delnan的观点 : Unicode很复杂。 您确定要将char (对应于代码点)作为单元而不是字形集群吗? 这个答案的其余部分假设你有充分的理由使用char而不是graphemes 。 考虑到Rust将字符串视为Unicode,这令人费解 这是不正确的; Rust将字符串视为UTF-8 。 在UTF-8中,每个代码点都映射到可变数量的字节。 没有O(1)算法将“6个字符”转换为“N个字节”,所以标准库不会将其隐藏起来。 您可以使用char_indices按字符char_ind

...

str.erase(

std::unique(str.begin(), str.end()),

str.end());

这不仅仅适用于空间。 例如,字符串“aaabbbcccddd”将变为“abcd”。 那是你要的吗? 如果您只想将空间减少到一个空格,可以将二元谓词作为第三个参数传递给std::unique ,如下所示: bool BothAreSpaces(char lhs, char rhs)

{

return (lhs == ' ') && (rhs == ' ')

...

试试这个代码 String[] givenString={"VACUTAINER [3X10] - 30RS","VACUTAINER[4 X10] - 40RS"};

//or use loop

String newStringFirst=givenString[0].substring(givenString[0].lastIndexOf("-") + 1).trim();

String newStringSecond=g

...

而不是FrameLayout,这是LinearLayout或RelativeLayout与ellipsize结合使用的完美场所:

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

...

android:width="0dp"

android:h

...

如果您尝试在第10个字符后按空格截断相册/标题,则可以使用indexOf方法 var album = " This is the're long title of the album ";

album = $.trim(album); //make sure to trim the album first

if (album.length > 10) {

//the indexOf will return the position of the first space starti

...

尝试使用dirname(3)因为你在Linux上。 由SUSv3指定,它非常便携。 char *dirname(char *path);

在通常情况下,dirname()返回字符串,但不包括最终的'/'。 Try dirname(3) since you are on Linux. Being specified by SUSv3, it's quite portable. char *dirname(char *path);

In the usual case, dirname() retu

...

你可以使用正则表达式来实现: var date_format="%m/%d/%Y";

var res = date_format.replace(/%(.)/g, "$1$1");

console.log(res); You can use a regular expression for that: var date_format="%m/%d/%Y";

var res = date_format.replace(/%(.)/g, "$1$1");

console.log(res

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值