new TestView()
을 통해 객체를 만들 수있는 사용자 정의보기 TestView
클래스를 만들고 싶습니다. 그러나 새 뷰 클래스에는 AttributeSet 객체가 필요합니다. 어디서부터 AttributeSet을 얻을 수 있습니까? 포함해야하는 것은 무엇입니까?사용자 정의보기 만들기
3
A
답변
10
super()
을 전달하는 View
의 생성자를 제공하는 한 대부분의 경우에는 걱정할 필요조차 없습니다.
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:*
모든 속성을 다루는 무거운 일을 담당한다. 당신이 그들을 정의한 경우 생성자는 그 속성이나 자신의 사용을 만들 수 : 뷰 클래스가 제공하는 3 생성자 구현 될 수
<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"
/>
0
하나의 .. 그래서, 속성으로 생성자를 제공하는 것이 필수가 아닙니다.
나는 attrs를 즉석에서 생성하려고 많은 시간을 보냈다. 나는이를 수행하는 방법에 대한 문서 나 예제를 찾는 것이 거의 불가능하다는 것을 알았다. 좋은 질문은 attrs를 사용하지 않을 때 foo와 quux 속성을 설정하고 새로운 CustomView()를 사용하는 것입니다. – Emile
CustomView (문자열 foo, 문자열 bletch). – Emile
질문과 답변을 게시하겠습니다. – Blrfl