android 最大宽度和高度是多少,Android 自定义最大宽度和高度的RecyclerView

原理就不再做说明了,相信了解自定义View的都可以看明白.

一. 代码部分

class AtMostRecyclerView : RecyclerView {

private var maxHeight = LayoutParams.MATCH_PARENT

private var maxWidth = LayoutParams.MATCH_PARENT

constructor(context: Context) : this(context, null)

constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

constructor(context: Context, attributeSet: AttributeSet?, defStyle: Int) : super(

context,

attributeSet,

defStyle

) {

val a =

context.obtainStyledAttributes(attributeSet, R.styleable.AtMostRecyclerView, defStyle, 0)

maxHeight = a.getDimensionPixelSize(R.styleable.AtMostRecyclerView_maxHeight, maxHeight)

maxWidth = a.getDimensionPixelSize(R.styleable.AtMostRecyclerView_maxWidth, maxWidth)

a.recycle()

}

override fun onMeasure(widthSpec: Int, heightSpec: Int) {

var tempWidthSpec = widthSpec

if (maxWidth > 0) {

tempWidthSpec = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST)

}

var tempHeightSpec = heightSpec

if (maxHeight > 0) {

tempHeightSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST)

}

super.onMeasure(tempWidthSpec, tempHeightSpec)

}

}

二.自定义属性

三.xml中使用

android:id="@+id/amrv_content_list"

android:layout_width="match_parent"

android:layout_height="wrap_content"

app:maxHeight="120dp"

app:maxWidth="220dp"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值