android自定义视图onmeasure TextView

时间:2015-10-30 08:51:50

标签: java android

我尝试做自定义组件。我扩展了View类并在方法覆盖onMeasure中做了一些TextView?我见过几个教程,但每个教程都有点不同。有时他们最后会调用super.onMeasure,有时他们使用setMeasuredDimension并且没有调用它。 onMeasure尺寸中图像和文本视图的区别在哪里?

my cod for formeasure 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 个答案:

答案 0 :(得分:1)

编辑

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

super.onMeasure(widthMeasureSpec, widthMeasureSpec);

答案 1 :(得分:0)

这是一个ImageView或TextView?

在片段或活动中创建对象时,可以根据需要设置宽度,重量和高度。

类似的东西:

SquareImageView squareImageView = findViewById(R.id.picture);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height);
squareImageView.setLayaoutParams(layoutParams);