当前位置: 首页 > news >正文

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

关于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.mrgr.cn/news/45146.html

相关文章:

  • 【自动驾驶】《Planning-oriented Autonomous Driving》UniAD论文阅读笔记
  • AI绘图创造无限可能,以参考图片为灵感生成图片
  • scrapy 爬取微博(六)【最新超详细解析】:情感分析+保存数据库
  • BeeS CMS 靶场漏洞攻略
  • 33 基于单片机的智能窗帘控制系统
  • 华为OD七日集训第4期 - 按算法分类,由易到难,循序渐进,玩转OD
  • 【大数据应用开发】2023年全国职业院校技能大赛赛题第04套
  • 基于java SpringBoot和Vue校园求职招聘系统设计
  • PHP 表单基础
  • 【人工智能】Transformers之Pipeline(一):音频分类(audio-classification)
  • 如何从数码相机中恢复已删除的照片
  • 机器学习:开启智能时代的钥匙
  • Python网络爬虫从入门到实战
  • Linux 命令 netstat 的 10 个基本用法
  • 机房空调远程控制-Thingsboard MQTT 接口说明
  • 车载音频焦点(二)
  • C++11标准模板(STL)- 常用数学函数 - 计算反正切,以符号确定象限(std::atan2, std::atan2f, std::atan2l)
  • 招联金融2025秋招内推
  • 华为OD机试 - 优雅子数组 - 暴力枚举(Python/JS/C/C++ 2024 E卷 100分)
  • 六、索引的数据结构