【Android】限制TextView大小并允许滑动

ops/2024/10/17 14:25:23/
关于TextView大小限制

TextView本身支持大小限制,但只支持固定值

这里改用屏幕比例来判断,按照屏幕剩余空间的一定比例来现在TextView最大尺寸

TextView滑动

当TextView空间不足时,需要通过滑动来查看剩余文本

TextView默认是禁用滑动特性的,可通过以下代码开启

movementMethod = ScrollingMovementMethod()
自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"><attr name="basicWidth" format="reference|dimension" /><attr name="basicHeight" format="reference|dimension" /><attr name="maxScreenRatioX" format="float" /><attr name="maxScreenRatioY" format="float" /><declare-styleable name="MaxSizeTextView"><attr name="basicWidth" /><attr name="basicHeight" /><attr name="maxScreenRatioX" /><attr name="maxScreenRatioY" /></declare-styleable>
</resources>
自定义控件
import android.content.Context
import android.text.method.ScrollingMovementMethod
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextViewclass MaxSizeTextView : AppCompatTextView {private var basicWidth = 0fprivate var basicHeight = 0fconstructor(context: Context) : this(context, null)constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {parseAttribute(attrs)movementMethod = ScrollingMovementMethod()}private fun parseAttribute(attrs: AttributeSet?) {val typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaxSizeTextView)if (typedArray.hasValue(R.styleable.MaxSizeTextView_basicWidth)) {basicWidth = typedArray.getDimension(R.styleable.MaxSizeTextView_basicWidth, 0f)}if (typedArray.hasValue(R.styleable.MaxSizeTextView_basicHeight)) {basicHeight = typedArray.getDimension(R.styleable.MaxSizeTextView_basicHeight, 0f)}if (typedArray.hasValue(R.styleable.MaxSizeTextView_maxScreenRatioX)) {val availableWidth = getScreenContentSize().width - basicWidthval ratioX = typedArray.getFloat(R.styleable.MaxSizeTextView_maxScreenRatioX, 0f)maxWidth = (availableWidth * ratioX).toInt()}if (typedArray.hasValue(R.styleable.MaxSizeTextView_maxScreenRatioY)) {val availableHeight = getScreenContentSize().height - basicHeightval ratioY = typedArray.getFloat(R.styleable.MaxSizeTextView_maxScreenRatioY, 0f)maxHeight = (availableHeight * ratioY).toInt()}typedArray.recycle()}
}
工具类
fun Context.getScreenWidth(): Float {return resources.displayMetrics.widthPixels.toFloat()
}fun Context.getScreenHeight(): Float {return resources.displayMetrics.heightPixels.toFloat()
}fun Context.getScreenContentSize() = Size().apply {width = getScreenWidth().toInt()height = getScreenHeight().toInt()
}
使用
<com.android.ui.view.MaxSizeTextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"app:basicHeight="360dp"app:maxScreenRatioY="0.7" />

http://www.ppmy.cn/ops/124291.html

相关文章

QTday4

数据库头文件 #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include<QSqlDatabase> //数据库管理类 #include<QSqlQuery> //数据库查询类 #include<QSqlRecord> //记录类QT_BEGIN_NA…

使用aloam跑hesai Pandar-XT32激光雷达数据

参考自利用aloam跑数据集_aloam数据集-CSDN博客 第一步&#xff1a;查看bag的信息 输入rosbag info来查看bag包的信息&#xff1a; joeyjoey-Legion-Y7000P-IRX9:~$ rosbag info /home/joey/Downloads/data2022/indoor/LiDAR_IMU.bag path: /home/joey/Downloads/da…

【aws】从s3里拉取驱动 需要后台创建凭证

简答&#xff1a;建一个有s3readonlyaccess的role&#xff0c;绑定给e2就好了 详细步骤&#xff1a; 1.在控制台搜IAM----左侧导航栏点role/角色----右上角创建角色 2.使用案例里选EC2 3.搜s3readonlyaccess这个策略----创建角色 4.选中指定实例&#xff0c;设置&#xff0c;绑…

项目——超级马里奥——Day(2)

争取今天晚上能搞一半啊&#xff0c;啊啊啊啊&#xff0c;感觉事多的忙不过来 设计思路&#xff1a; 1&#xff09;创建并完成常量类 ------->一张图片的情况 先完成对图片的封装------>把图片加载一遍 &#xff08;老实说&#xff0c;我也不太知道为什么&#xff0…

【使用Java循环输出菱形,空心金字塔】

使用Java循环输出图形的探索之旅 在这篇博客中&#xff0c;我们将探讨如何使用Java中的循环结构来输出各种几何图形&#xff0c;特别是金字塔和菱形。通过这一过程&#xff0c;不仅能够加深对循环的理解&#xff0c;还能提升编程能力。 1. 打印矩形 首先&#xff0c;我们从最…

UE5.3.2查看引擎真正版本

编译好的插件给别人用&#xff0c;发现引擎不一致&#xff0c;而且双方都是5.3.2版本引擎 打开Help->About Unreal Editor可以看到引擎版本 或者直接查看引擎版本文件&#xff1a;XXXXX\Engine\Build\Build.version 里面能看到对应的分支名字

2024Java最新面试题总结(针对于一些小厂、中厂)

这是根据个人面试经历总结出来的一些经验希望可以帮助到有需要的人。 面试的时候&#xff0c;会先让你进行自我介绍&#xff0c;这个大家准备一两分钟的面试稿就可以。然后就是正式面试&#xff0c;面试官一般是两个人以上&#xff0c;开始&#xff0c;面试官会先提问一些基本…

使用CSS实现酷炫加载

使用CSS实现酷炫加载 效果展示 整体页面布局 <div class"container"></div>使用JavaScript添加loading加载动画的元素 document.addEventListener("DOMContentLoaded", () > {let container document.querySelector(".container&q…