【发布时间】:2023-03-15 02:17:01
【问题描述】:
我尝试做自定义组件。我扩展了 View 类并在方法覆盖 onMeasure 中做了一些 TextView?我看过几个教程,但每个教程都有点不同。有时他们在最后调用 super.onMeasure,有时他们使用 setMeasuredDimension 而没有调用它。 onMeasure size 中的 image 和 textview 的区别在哪里?
我对 SquareImageView 的 onmeasure ImageView 的鳕鱼
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
//Snap to width
}
}
如何在TextView 中设置动态高度?
<com.SquareImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
【问题讨论】:
-
请分成两个问题!