当前位置:网站首页>直播电商平台开发,点击查看更多显示所有内容
直播电商平台开发,点击查看更多显示所有内容
2022-08-09 05:39:00 【云豹网络科技】
直播电商平台开发,点击查看更多显示所有内容
实现代码:
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来进行字符串的截取操作,再设置全显示还是部分显示了。
以上就是 直播电商平台开发,点击查看更多显示所有内容,更多内容欢迎关注之后的文章
边栏推荐
- 微服务架构基础 微服务相关概念及基础知识 贺兰的微博
- A day to learn a public company: Sophia
- 【LeetCode】1283. 使结果不超过阈值的最小除数
- pytorch implements GAN entry case
- Still don't know what business intelligence (BI) is?After reading this article, you will understand
- X86汇编语言从实模式到保护模式练习题
- 初识二叉树
- Chapter 3 Search and Graph Theory (1)
- 找两个单身狗
- CSDN使用方法
猜你喜欢
随机推荐
10.LoadRunner2022社区版安装
STM32系列单片机使用心得
【LeetCode】1283. 使结果不超过阈值的最小除数
RT201 国产PA射频功率放大器 兼容RFX2401C
2022-08-08 第四小组 修身课 学习笔记(every day)
图解LeetCode——761. 特殊的二进制序列(难度:困难)
如何让Win11两个屏幕任务栏都显示时间?
【零基础玩转BLDC系列】无刷直流电机闭环控制与软件架构
Build a "firewall" for safety and carry out firefighting training in Fengzhuang Township, Tongxu County, Henan Province
Why do enterprises need business intelligence BI in the digital age
学习一下 常说的节流
flask——介绍、安装、快速使用、配置文件、路由系统、视图
什么是SIP请求和SIP响应?
shell function
查询的结果封装到实体类中并使用集合储存
详谈归并排序时间复杂度过程推导----软考
剑指Offer-二叉树路径问题总结
Transaction rolled back because it has been marked as rollback-only
MATLAB图像处理入门
剑指Offer-双指针类型题目总结