我试图做自定义组件。我扩展了View类,并且在方法中做了一些TextView重写onMeasure吗?我看过一些教程,但是每个教程都有点不同。有时他们在最后调用super.onMeasure,有时他们使用setMeasuredDimension而没有调用它。 onMeasure大小的图像和文本视图在哪里?

我的鳕鱼onSquareImageView的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"/>

最佳答案

编辑

 super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());




super.onMeasure(widthMeasureSpec, widthMeasureSpec);

关于java - Android自定义 View onmeasure TextView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33431972/

10-11 20:00