ImageView宽固定,高自适应/高固定,宽自适应
解决方法
宽固定,高自适应
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxWidth="0dp"
android:maxHeight="1000dp"
android:adjustViewBounds="true"/>
高固定,宽自适应
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxWidth="1000dp"
android:maxHeight="0dp"
android:adjustViewBounds="true"/>
也可以自己写个正方形的ImageView
class SquareImageView : AppCompatImageView {
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) {}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val width = MeasureSpec.getSize(widthMeasureSpec)
val mheightMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY)
super.onMeasure(widthMeasureSpec, mheightMeasureSpec)
}
}
怎么用? 结合dataBinding,glide,可美观了
<com.sq.module.view.SquareImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/library.icon.marginBottom"
android:scaleType="centerCrop"
app:imageUrl="@{item.images}"/>