【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" />
相关推荐
一笑的小酒馆5 小时前
Android性能优化之截屏时黑屏卡顿问题
android
懒人村杂货铺8 小时前
Android BLE 扫描完整实战
android
TeleostNaCl10 小时前
如何安装 Google 通用的驱动以便使用 ADB 和 Fastboot 调试(Bootloader)设备
android·经验分享·adb·android studio·android-studio·android runtime
fatiaozhang952711 小时前
中国移动浪潮云电脑CD1000-系统全分区备份包-可瑞芯微工具刷机-可救砖
android·网络·电脑·电视盒子·刷机固件·机顶盒刷机
2501_9159184112 小时前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
lichong95112 小时前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之dist打包发布在Android工程asserts里
android·vue.js·iphone
Android出海12 小时前
Android 15重磅升级:16KB内存页机制详解与适配指南
android·人工智能·新媒体运营·产品运营·内容运营
一只修仙的猿12 小时前
毕业三年后,我离职了
android·面试
编程乐学12 小时前
安卓非原创--基于Android Studio 实现的新闻App
android·ide·android studio·移动端开发·安卓大作业·新闻app
雅雅姐13 小时前
Android14 init.rc中on boot阶段操作4
android