【问题标题】:android custom view onmeasure TextViewandroid自定义视图onmeasure TextView
【发布时间】: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"/>

【问题讨论】:

  • 请分成两个问题!

标签: java android


【解决方案1】:

编辑

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

super.onMeasure(widthMeasureSpec, widthMeasureSpec);

【讨论】:

    【解决方案2】:

    这是 ImageView 还是 TextView?

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

    类似的东西:

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

    【讨论】:

    • 但是如果你需要xml中的宽度,宽度不能是动态的。您可以将 xml 中的宽度作为 match_parent (例如),然后在代码中进行更改。
    猜你喜欢
    • 2012-08-29
    • 2015-02-02
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多