Android中TextView内容过长加省略号

textview中有个内容过长加省略号的属性,即ellipsize,用法如下: 
在xml中 
Android:ellipsize = "end"    省略号在结尾 
android:ellipsize = "start"   省略号在开头 
android:ellipsize = "middle"     省略号在中间 
android:ellipsize = "marquee"  跑马灯 
最好加一个约束android:singleline = "true" 

也可以用代码语句 

tv.setEllipsize(TextUtils.TruncateAt.valueOf("END")); 
tv.setEllipsize(TextUtils.TruncateAt.valueOf("START")); 
tv.setEllipsize(TextUtils.TruncateAt.valueOf("MIDDLE")); 
tv.setEllipsize(TextUtils.TruncateAt.valueOf("MARQUEE")); 
最好再加一个约束tv.setSingleLine(true); 
不仅对于textview有此属性,对于editext也有,不过它不支持marquee
linux
Android中,实现TextView中文字过长显示省略号并支持点击展开的功能,通常通过设置`android:singleLine="false"`属性以及监听`android:focusable="true"`和`android:focusableInTouchMode="true"`来实现。以下是基本步骤: 1. **布局设计**: 在XML布局文件中,给TextView适当的属性: ```xml <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一段非常非常长的文字..." android:maxLines="5" // 设置最大行数,默认为1,超过会截断 android:ellipsize="end" // 设置超出部分显示省略号 android:focusable="true" android:focusableInTouchMode="true"/> ``` 2. **点击事件处理**: 在对应的Activity或Fragment的Java或Kotlin代码中,为TextView设置点击监听器,例如: ```java TextView textView = findViewById(R.id.text_view); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!textView.isExpanded()) { // 展开文本 textView.setMaxLines(Integer.MAX_VALUE); // 展开到最后一行 } else { // 缩回文本 textView.setMaxLines(5); // 回收到默认限制 } textView.setEllipsize(null); // 展开时去掉省略号 textView.requestLayout(); // 强制布局更新 textView.isExpanded() ? textView.setText("展开更多") : textView.setText(textView.getText()); } }); ``` 在这个例子中,`isExpanded()`方法用于检查当前状态,并相应地调整TextView的行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值