当前位置: 首页> 游戏> 攻略 > 宁波网络推广培训_如何建设公司网站_建站为应用技术_网站建设品牌公司

宁波网络推广培训_如何建设公司网站_建站为应用技术_网站建设品牌公司

时间:2025/7/10 1:53:11来源:https://blog.csdn.net/u013718730/article/details/142752733 浏览次数:2次
宁波网络推广培训_如何建设公司网站_建站为应用技术_网站建设品牌公司
关于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.AppCompatTextViewclass MaxSizeTextView : AppCompatTextView {private var basicWidth = 0fprivate var basicHeight = 0fconstructor(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 - basicWidthval ratioX = typedArray.getFloat(R.styleable.MaxSizeTextView_maxScreenRatioX, 0f)maxWidth = (availableWidth * ratioX).toInt()}if (typedArray.hasValue(R.styleable.MaxSizeTextView_maxScreenRatioY)) {val availableHeight = getScreenContentSize().height - basicHeightval 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.MaxSizeTextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"app:basicHeight="360dp"app:maxScreenRatioY="0.7" />
关键字:宁波网络推广培训_如何建设公司网站_建站为应用技术_网站建设品牌公司

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: