【问题标题】:Pass data to custom View class将数据传递给自定义 View 类
【发布时间】:2016-09-14 22:11:53
【问题描述】:

我想将一些数据传递给扩展 android.view.View 的自定义类。 但是,我收到一条警告消息:

自定义视图 LinePlot 缺少工具使用的构造函数:(上下文) 或 (Context,AttributeSet) 或 (Context,AttributeSet,int)

但是,我运行了代码,一切似乎都很顺利。

  • 发生了什么?
  • 我应该如何将我的数据传递给我的班级?
  • 为什么我不能使用自定义构造函数?

谢谢!

import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

import java.util.ArrayList;

public class LinePlot extends View {

    private ArrayList<Float> mPoints;
    private int dx;
    private int dy;

    Paint paint=new Paint();

    public LinePlot(Context context,int dx_plot, int dy_plot, ArrayList<Float> points) {

        super(context);
        mPoints=points;
        dx=dx_plot;
        dy=dy_plot;
    }


@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    // plotting my data here
}  


}

【问题讨论】:

  • 您打算如何创建自定义View 的实例?通过布局xml文件?还是以编程方式?
  • 我将以编程方式创建我的自定义 View 的实例。我的 .xml 中有一个 FrameLayout,我将通过执行 frameLayout.addView(linePlot) 添加 LinePlot 对象

标签: android android-view


【解决方案1】:

所以这里必须遵循几个步骤 -

(i) 在 values/attrs.xml 添加以下代码 -

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="NinjaView">
       <attr name="addButtonBackgroundDrawable" format="integer"/>
    </declare-styleable>
</resources>

(ii) 在 layout.xml 文件中调用自定义视图。例如-

<com.example.act.ui.utils.NinjaView
                        android:id="@+id/ninja_view_product_desc"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        custom:addButtonBackgroundDrawable="@drawable/add_button_back_about_product"/>

(不要忘记声明您的自定义命名空间:xmlns:custom="http://schemas.android.com/apk/res-auto")

(iii) 在自定义视图的类中,您必须添加一些内容。示例 -

public class NinjaView extends LinearLayout {

@BindView(R.id.button_add_to_cart_product) Button mAddToCart;

public NinjaView(Context context) {
    super(context);
    initializeNinjaView(context);
}

public NinjaView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initializeNinjaView(context);
    setAddButtonBack(context, attrs);
}

public NinjaView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initializeNinjaView(context);
    setAddButtonBack(context, attrs);
}

private void initializeNinjaView(Context context) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.ninja_view, this);
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    ButterKnife.bind(this);
}

private void setAddButtonBack(Context context, AttributeSet attrs) {
    TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.NinjaView, 0, 0);
    Drawable backgroundDrawable;
    try {
        backgroundDrawable = typedArray.getDrawable(R.styleable.NinjaView_addButtonBackgroundDrawable);
    } finally {
        typedArray.recycle();
    }
    if (backgroundDrawable != null) {
        (findViewById(R.id.button_add_to_cart_product)).setBackground(backgroundDrawable);
    }
}

【讨论】:

    【解决方案2】:

    警告意味着您将无法在任何 xml 布局中使用自定义视图。

    如果您不打算这样做,最好为您的自定义视图实现这些构造函数,如下所示:

    CustomView(Context ctx) {
       super(ctx)
    }
    

    任何附加属性通常作为自定义属性而不是构造函数参数传递。阅读文档或其他地方的自定义视图属性http://www.tutorialspoint.com/android/create_custom_attributes_for_custom_component.htm

    【讨论】:

      【解决方案3】:

      在你的 LinePlot 类中添加构造函数:

      public LinePlot(Context context) {
        super(context);
      }
      public LinePlot(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
      

      【讨论】:

        【解决方案4】:

        你需要添加带参数的构造函数 (Context), (Context,AttributeSet) 和 (Context,AttributeSet,int)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-07-04
          • 2018-05-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-06
          • 2021-11-05
          相关资源
          最近更新 更多