안드로이드 개발 질문/답변
(글 수 45,052)
현재 XML이 위 그림과 같이 구성되어있습니다.
레이아웃 안에 TextView와 View가 있는데요 아래쪽 View에 onDraw를 쓰고 싶어서 View를 넣어두었는데요
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.drawled); <--- 이 부분이 위의 xml 입니다.
}위의 XML View 안에 아래에 있는 클래스를 넣어서 ondraw 하기를 원하는데요
어떻게 불러줘야 할까요;;
public class DrawLedView extends View{
private Paint Pnt = null;
public DrawLedView(Context context) {
super(context);
Pnt = new Paint();
/ / TODO Auto-generated constructor stub
}
@Override
public void onDraw(Canvas canvas)
{
canvas.drawColor(Color.RED);
Pnt.setAntiAlias(true);
Pnt.setColor(Color.WHITE);
canvas.drawCircle(150, 250, 15, Pnt);
}
}
2010.10.29 00:28:41
답변 감사합니다~
View 태그를 xxx.xxxx.xxxxx.DrawLedView 로 바꾸시라는것은
Properties에서 tag 항목에 저렇게 바꾸라는것이지요?
그리고 생성자를 public DrawLedView(Context context, AttributeSet attrs) 이렇게 만들어준다면
DrawLedView 를 부를때는 어떻게 인자를 넣어줘서 불러야하나요?
메인 클래스 에서 보통은
DrawLedView DLV = new DrawLedView(this);
일허게 부르는데 인자가 두개면 어떻게 하나요;;
그리고 new해서 DrawLedView class를 한번 불러와놓고
setContentView(R.layout.해당xml); 이렇게 불러주면 되는것인가요?




저도 비슷한 고민을 가지고 있던 적이 있어서 도움이 될꺼 같네요...
일단 XML에서는 View 태그를 아래와 같이 바꿔야 합니다.
물론, xxx.xxxx.xxxx 는 만드신 패키지 명을 적으시면 되구요..
<xxx.xxxx.xxxxx.DrawLedView
위와 같이만 하고 실행하시면 에러가 발생을 하는데...이때 로그캣에 다음과 같은 에러가 보일겁니다..
ERROR/AndroidRuntime(398): Caused by: java.lang.NoSuchMethodException: DrawLedView(Context,AttributeSet)
그래서 레퍼런스를 뒤져보면 아래와 같은 내용이 존재합니다.
즉, xml에서 해당 클래스를 사용할때는 Context, AttributeSet 을 인자로 갖는 생성자가 있어야만 한다 라는 내용입니다.
=================================================================
Constructor that is called when inflating a view from XML. This is called when a view is being constructed from an XML file, supplying attributes that were specified in the XML file. This version uses a default style of 0, so the only attribute values applied are those in the Context's Theme and the given AttributeSet.
The method onFinishInflate() will be called after all children have been added.
View(Context, AttributeSet, int)=================================================================
그래서 아래와 같은 생성자를 하나만 더 만들어준뒤 실행해보면..동작을 하겠죠...^^
public DrawLedView(Context context, AttributeSet attrs) {
super(context, attrs);
Pnt = new Paint();
/ / TODO Auto-generated constructor stub
}