2016-05-18 7 views
2

android.view.Viewを拡張するカスタムクラスにいくつかのデータを渡したいと思います。カスタムViewクラスにデータを渡す

カスタムビューLinePlotはツールによって使用コンストラクタ欠落しています:(コンテキスト) または(コンテキスト、属性セット)または(コンテキスト、属性セット、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 
} 


} 
+1

カスタム 'View'のインスタンスをどのように作成しますか?レイアウト 'xml'ファイルを通して?またはプログラム的に? –

+0

カスタム 'View'のインスタンスをプログラムで作成します。私は自分の.xmlに 'FrameLayout'を持っています。' frameLayout.addView(linePlot) 'を実行して' LinePlot'オブジェクトを追加します。 –

答えて

0

警告は、任意のxmlレイアウトでカスタム表示を使用できないことを意味します。あなたはそれがこのようなカスタムビューのために、これらのコンストラクタを実装するために、まだ良いことだそれを行うことを意図していない場合

:カスタムコンストラクタのパラメータとしてではない属性として

CustomView(Context ctx) { 
    super(ctx) 
} 

任意の追加の属性が正常に渡されます。ドキュメントにカスタムビューattribytesで読むか、他の場所http://www.tutorialspoint.com/android/create_custom_attributes_for_custom_component.htm

0

あなたLinePlotクラスのコンストラクタを追加します。

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

あなたは(コンテキスト、属性セット)と(コンテキスト、パラメータ (コンテキスト)でコンストラクタを追加する必要があり、属性セット、INT)

0

ので、ステップのいくつ持っているためにここに続く -

(I)の値に/ attrs.xmlは、次のコードを追加します -

(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:カスタム= "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); 
    } 
} 
関連する問題