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

【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.AppCompatTextView

class MaxSizeTextView : AppCompatTextView {

    private var basicWidth = 0f
    private var basicHeight = 0f

    constructor(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 - basicWidth
            val ratioX = typedArray.getFloat(R.styleable.MaxSizeTextView_maxScreenRatioX, 0f)
            maxWidth = (availableWidth * ratioX).toInt()
        }
        if (typedArray.hasValue(R.styleable.MaxSizeTextView_maxScreenRatioY)) {
            val availableHeight = getScreenContentSize().height - basicHeight
            val 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.MaxSizeTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:basicHeight="360dp"
    app:maxScreenRatioY="0.7" />

http://www.kler.cn/news/343045.html

相关文章:

  • 基于SpringBoot vue 医院病房信息管理系统设计与实现
  • 高级java每日一道面试题-2024年10月5日-数据库篇[MySQL篇]-MySQL为什么InnoDB是默认引擎?
  • 新版 Notepad++ 下载与安装教程
  • MES系统中人机接口设计和开发研究
  • Windows11 24H2 专业工作站版:安全稳定,值得信赖!
  • 《大规模语言模型从理论到实践》第一轮学习--分布式训练
  • 【Linux-基础IO】磁盘的存储管理详解
  • 求职书与求职经历 - Chap01.
  • 图文深入理解Oracle DB Scheduler(续)-调度的创建
  • Java面试宝典-Java集合02
  • Python进阶--正则表达式
  • Linux-基础(CentOS8)
  • Chromium 如何构建一个单独exe c++
  • Linux 外设驱动 应用 1 IO口输出
  • 设计算法int IsExistEL(MGraph G),判断G是否存在EL路径,若存在,则返回1 ,否则返回0。
  • 【Linux】Windows搭建CentOS7环境
  • 多模态智能
  • 鸿蒙NEXT开发-动画(基于最新api12稳定版)
  • 【C++设计模式】结构型模式:桥接模式
  • Leetcode 第 417 场周赛题解