안드로이드 개발 질문/답변
(글 수 45,052)
일반적인 리스트 뷰와 비슷한 구조입니다.
[다수의 뷰] ----AddView()---> LinearLayout ---AddView()---> Scrollview
의 구조로 해서 리스트 뷰처럼 구현을 하려고 하는데요,
단지, View에 Canvas를 이용하여 그림을 그리고 LinearLayout 에 추가하고 싶어서 OnDraw() 부분을
오버라이딩했습니다.
아래는 XML에서 정의한 LinearLayout을 가져와서 뷰를 만들고 테스트를 위해서 LinearLayout 에 40개쯤 넣는 소스입니다.
LinearLayout lv = (LinearLayout)findViewById( R.id.calendar_lLayout03 ) ; for(int i = 0 ; i < 40 ; i++){ Days_View TV = new Days_View(getApplicationContext(), i , 480); TV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); lv.addView(TV); }
아래가 제가 정의한 View의 소스구요.
public class Days_View extends View{ private Paint paint; private int time; private float left; private float top; private float width;
public Days_View(Context context, int tim, float wid) { super(context); // TODO Auto-generated constructor stub paint = new Paint(); time = tim; left = (float)10.0; top = (float)30.0; width = wid; } @Override protected void onDraw(Canvas canvas) { // draw lable paint.setColor(Color.WHITE); paint.setTextSize(20); canvas.drawText(String.valueOf(time+1), left, top , paint); // Line canvas.drawLine(50,10,width,10, paint); // define. } }
문제는 View 내의 OnDraw가 호출되지 않는다는데에 있습니다. 혹시나 해서 뷰를 new로 생성하고
Invalidate() 해보았으나 소용이 없네요.
아마도 SetContentView()와 같은 함수로 호출해주지 않으니 그런 것 같은데....
하고자하는 것은 뷰에 선을 긋고 글짜를 조금 쓰면 됩니다.
다른 방향으로 돌려서 이게 가능할까요?