我试着做自定义组件 . 我扩展了View类并在方法覆盖onMeasure中做了一些TextView?我见过几个教程,但每个教程都有点不同 . 有时他们最后会调用super.onMeasure,有时他们使用setMeasuredDimension并且没有调用它 . onMeasure尺寸中的图像和文本视图的区别在哪里?
我的鳕鱼为onmeasure ImageView for SquareImageView
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"/>
2 回答
编辑
至
这是一个ImageView或TextView?
在片段或活动中创建对象时,可以根据需要设置宽度,重量和高度 .
像这样的东西: