当前位置:网站首页>TextView展示不完的内容实现--全显示、部分显示
TextView展示不完的内容实现--全显示、部分显示
2022-07-22 22:39:00 【Rannki】
内容全展开前:

内容全展开后:

实现代码:
mainactivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="280dp"
android:text="1234567890123456789012345678901234567890"
android:background="@color/teal_200"
android:textColor="@color/black"
android:layout_gravity="center_horizontal" />
<ImageView
android:visibility="gone"
android:layout_marginEnd="10dp"
android:id="@+id/show"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentEnd="true"
android:src="@mipmap/ic_down_arrow" />
</RelativeLayout>MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获得控件
TextView textView = findViewById(R.id.text);
ImageView imageView = findViewById(R.id.show);
// 需要显示的文本
String text = "1234567890123456789012345678901234567890";
// 最大显示行数
int maxLine = 2;
// 等待textview绘制完成后,执行
textView.post(new Runnable() {
@Override
public void run() {
// 获取TextView的画笔对象
TextPaint paint = textView.getPaint();
// 获取textview的宽度,单位是px
int width = textView.getWidth();
// 实例化StaticLayout 传入相应参数
StaticLayout staticLayout = new StaticLayout(text, paint, width, Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
// 获得文本所需显示得line行数
int lineCount = staticLayout.getLineCount();
// 如果需要显示的行数大于了规定的最大显示行数
if (lineCount > maxLine) {
// 显示下箭头的imageview
imageView.setVisibility(View.VISIBLE);
// 获取到第大行数最后一个文字的下标
int index = staticLayout.getLineStart(maxLine) - 1;
// 将最后一行的内容删掉3个,给下箭头imageview留出一点空间
String textSub = text.substring(0, index - 3) + "...";
textView.setText(textSub);
// 获得下箭头imageview的LayoutParams
RelativeLayout.LayoutParams layoutParamsImageView = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
// 再实例化一个StaticLayout 传入相应参数
StaticLayout staticLayoutHeight = new StaticLayout(textSub, paint, width, Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
// 设置下箭头imageview距离分期期数内容的上间距
int height = staticLayoutHeight.getHeight();
layoutParamsImageView.topMargin = (int) (height - dp2px(MainActivity.this, 12f));
// 下箭头imageview的点击事件
String finalContentSub = textSub;
imageView.setOnClickListener(v -> {
// 内容是否已展开
if (!textView.isSelected()) {
// 内容还未展开
// 将所有的内容显示出来
textView.setText(text);
layoutParamsImageView.topMargin = staticLayout.getHeight();
// 标记评价内容已展开
textView.setSelected(true);
} else {
// 将截取后的内容显示出来
textView.setText(finalContentSub);
layoutParamsImageView.topMargin = (int) (staticLayoutHeight.getHeight() - dp2px(MainActivity.this, 12f));
// 标记内容未展开
textView.setSelected(false);
}
});
} else {
// 没有超过最大显示行数
textView.setText(text);
// 隐藏下箭头的imageview
imageView.setVisibility(View.GONE);
}
}
});
}
/**
* dp转为px
* @param context
* @param dipValue
* @return
*/
private float dp2px(Context context, float dipValue) {
if (context == null) {
return 0;
}
final float scale = context.getResources().getDisplayMetrics().density;
return (float) (dipValue * scale + 0.5f);
}
}就是用了一个【StaticLayout】对象,这个对象专门用来操作textview的,通过它,可以知道需要显示某文本时,所需要的行数,每一行的宽度、下标等。这样,就可以用subString来进行字符串的截取操作,再设置全显示还是部分显示了。
边栏推荐
- 第三章 栈
- 改变this指向了解一下
- Web资源共享
- Live broadcast preview | live broadcast Seminar on open source security governance models and tools
- [reading notes > statistics] 12-01 construction of confidence interval - Introduction to the concept of confidence interval
- appendToFile追加失败
- Jmeter查看结果树之查看响应的13种详解方法!
- 使用同一个接口实现不同登录的方式
- Arduino中断实现上升沿检测,并执行其他函数
- 这不是真正意义上的元宇宙,元宇宙应当具备自身鲜明的特质和独特的发展逻辑
猜你喜欢

appendToFile追加失败

Redis transaction and locking mechanism

Redis 事务学习有感

Mria + RLOG 新架构下的 EMQX 5.0 如何实现 1 亿 MQTT 连接

Qt+vtk+pcl pictures are converted to grayscale images and displayed with grayscale as the Y axis

How to use C language to realize simple employee information management system

Matlab保存数据到csv文件的方法分享
![[reading notes > statistics] 12-01 construction of confidence interval - Introduction to the concept of confidence interval](/img/27/0431eeedbffdba1bdf4bb015082219.png)
[reading notes > statistics] 12-01 construction of confidence interval - Introduction to the concept of confidence interval

Web resource sharing

动作捕捉在自动化控制领域的应用
随机推荐
RequestContextHolder
Arduino中断实现上升沿检测,并执行其他函数
Live broadcast preview | live broadcast Seminar on open source security governance models and tools
How to use C language to realize simple employee information management system
SQL报错盲注实例分析
高精度移相(MCP41xx)程序stm32F103,F407通用,更改引脚即可(SPI软件模拟通信)
开发者分享|『啃书吧:深度学习与MindSpore实践』第一章
Genesis公链:夯实Web 3.0发展底座
互联网流量编排方案
C语言中的字符串
Tensorrt plug-in practice (1)
Cloud computing may become a new inflection point in the era? From which point can we see?
Several ways of QT thread exit
Can PHP array subscripts only start from 0
Google Earth engine app - a complete map legend app (land use classification of western United States)
uni-app进阶之内嵌应用【day14】
postgresql数据库主从部署 主库挂了重新还原主库
Networkx visualizes graphs
押注全场景,荣耀胜算几何?
Celebrity interview | various strange current situations in the open source community -- night sky Book Chen Zili tison