【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" />
相关推荐
暮志未晚Webgl2 小时前
98. UE5 GAS RPG 实现闪电链技能负面效果
android·网络·ue5
千里马学框架2 小时前
安卓如何实现双击触摸唤醒点亮屏幕功能-源码分析linage os高通平台
android·智能手机·framework·锁屏·车机车载·双击·亮屏
命运之手2 小时前
【Android】Build Non-Fullscreen Transparent Activity
android·activity·dialog-style·non-fullscreen·transparent
kidding7233 小时前
uniapp打包安卓apk步骤
android·uni-app
趣浪出海3 小时前
Android 无Bug版 多语言设计方案!
android·bug·多语言·google play
程序猿陌名!3 小时前
Android 编译使用哪个key签名?
android
二流小码农3 小时前
鸿蒙开发:两个重磅更新,鸿蒙版微信要来了!
android·ios·harmonyos
通信侠3 小时前
android广播实现PIN码设置
android·java·经验分享
王能3 小时前
Kotlin真·全平台——Kotlin Compose Multiplatform Mobile(kotlin跨平台方案、KMP、KMM)
android·ios·kotlin·web·android jetpack·kmp·kmm