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

关于TextView大小限制

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

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

TextView滑动

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

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

kotlin 复制代码
movementMethod = ScrollingMovementMethod()
自定义属性
xml 复制代码
<?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>
自定义控件
kotlin 复制代码
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()
    }
}
工具类
kotlin 复制代码
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()
}
使用
xml 复制代码
<com.android.ui.view.MaxSizeTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:basicHeight="360dp"
    app:maxScreenRatioY="0.7" />
相关推荐
2301_8111305444 分钟前
【保姆级教程】Android Studio完整安装步骤(2026最新版,新手零踩坑)
android·java
帅次1 小时前
Android 高级工程师面试参考答案:项目经历、自我介绍与实战案例表达
android·面试·职场和发展
小猫爱游戏1 小时前
theone陪伴ai手机版免费版下载安装教程附带最新邀请码theone陪伴ai设定教程接入下载使用教程手机版安卓版app鸿蒙版苹果版IOS电脑版安装包下载地址
android·人工智能·智能手机·theone陪伴ai·theone陪伴ai下载·免费下载安装·接入微信教程
韩曙亮1 小时前
【Android】Android 源码查看 ( Android 源码在线查看 2026-03-30 | Android 源码下载 | Android 源码查看工具 )
android·安卓·安卓源码·aosp·android 源码·android源码查看工具·android 源码工具
游戏开发爱好者81 小时前
iOS应用性能监控:Pre-Main与Main函数耗时分析及Time Profiler使用教程
android·ios·小程序·https·uni-app·iphone·webview
dora2 小时前
从dorachat-auth的角度看登录认证
android
Fate_I_C2 小时前
View Binding的基础使用
android·kotlin·viewbinding
zhangphil2 小时前
Android Coil 3 extend ImageRequest‘s custom method/function,Kotlin
android·kotlin
星河漫步Lu2 小时前
QT6中五步完成Android的环境配置
android·qt