关于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" />

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部