4

TestViewを介してオブジェクトを作成できるカスタム ビュー クラスを作成したいと考えていますnew TestView()。ただし、新しいビュー クラスには AttributeSet オブジェクトが必要です。どこからその AttributeSet を取得し、何を含める必要がありますか?

4

2 に答える 2

10

Viewこれは必須ではありません。ほとんどの場合、 からに渡すコンストラクタを提供する限り、心配する必要さえありませんsuper()

public CustomView(Context context)  // No Attributes in this one.
{
  super(context);
  // Your code here
}

public CustomView(Context context, AttributeSet attrs)
{
  super(context, attrs);
  // Your code here
}

public CustomView(Context context, AttributeSet attrs, int default_style)
{
  super(context, attrs, default_style);
  // Your code here
}

Viewビューをレイアウトに追加するときに通常渡すすべての属性を処理するための面倒な作業を処理しandroid:*ます。コンストラクターは、これらの属性を使用することも、定義した場合は独自の属性を使用することもできます。

<com.blrfl.CustomView
 android:id="@+id/customid"
 android:layout_weight="1"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:layout_gravity="center"
 blrfl:foo="bar"
 blrfl:quux="bletch"
/>
于 2010-12-16T14:53:32.937 に答える
0

ビュー クラスが提供する 3 つのコンストラクターのいずれかを実装できます。そのため、コンストラクターに属性セットを提供することは必須ではありません。

于 2010-12-20T08:51:47.060 に答える