2011-12-06 2 views
3

동적으로 사용자 인터페이스를 만들려고합니다. 성공적으로 뷰를 만들고 배경 이미지를로드했습니다. 배경에 표시 할 두 개의 작은보기 항목을 추가로 만들었습니다. 내 문제는 내가 작은 관점을 그려내는 방법을 알려주는 충고/지시를 찾을 수 없었다는 것이다. 그것이 사소한 운동이어야하고 올바른 참조를 찾는 것 같아요. 누군가가 나를 올바른 방향으로 인도 할 수 있기를 바랍니다. 여기 활동에서 뷰 구성 요소를 동적으로 추가하는 방법

내 활동입니다 :

public class GhostActivity extends Activity implements OnTouchListener 
{ 
private DrawView ghostView; 
public Card mCard1, mCard2; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    // ToDo add your GUI initialization code here   
    super.onCreate(savedInstanceState); 
    // requesting to turn the title OFF 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // making it full screen 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    ghostView = new DrawView(this); 
    setContentView(ghostView); 

    //get the window size 
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 

    Context context = getApplicationContext(); 
    //create view items with initial positions 
    Point startPoint; 
    startPoint = new Point(); 
    startPoint.x = 5; 
    startPoint.y = 3; 
    mCard1 = new Card(context, 1, R.drawable.bol_geel, startPoint); 
    startPoint.x = 5; 
    startPoint.y = 43; 
    mCard2 = new Card(context, 2, R.drawable.bol_rood, startPoint); 

    //now display them on the ghostView *****************HOW? 

    // set the callbacks 
    ghostView.setOnTouchListener(this); 
    mCard1.setOnTouchListener(this); 
    mCard2.setOnTouchListener(this); 

    } 

여기보기입니다;

public class DrawView extends View 
{ 
Drawable bg ; 
public DrawView(Context context) { 
    super(context); 
    //setFocusable(true); 
    Drawable bg = this.getResources().getDrawable(R.drawable.bubbleblue480x800); 
    setBackgroundDrawable(bg); 
    } 


@Override protected void onDraw(Canvas canvas) { 
// canvas.drawColor(0x0000000);  //if you want another background color  

    //draw on the canvas 
    } 
} 

편집 : ghostView 캔버스에 대한 포인터를 전달해야 할 필요가 있다고 생각합니다. 내가 ghostView 내에서 자식을 만든 다음 .draw 메서드를 호출하면 예상대로 정확하게 나타납니다.

@Override protected void onDraw(Canvas canvas) { 
    canvas.drawColor(0x0000000);  //if you want another background color  

    //draw the cards on the canvas 
    mCard1.draw(canvas); 
    mCard2.draw(canvas); 
} 

그래서이 시점에서 ghostView 캔버스에 대한 참조 포인터를 얻는 방법에 대해 궁금합니다. 솔직히 전체 활동 -보기 관계가 혼란 스럽습니다.

편집 :이 튜토리얼 http://www.kellbot.com/2009/06/android-hello-circle/ 그것은 FrameLayout이 사용에 자세히에 따라 다른 접근 방식을 촬영하고 내가 내 목표를 달성 할 수있는 것 같다.

답변

0

보기를 동적으로 추가하여 ViewGroup 또는 LinearLayout 클래스에서 확장해야하는 경우 addView 메소드를 호출 할 수 있습니다.

+0

감사합니다. s_id, 나는 ViewGroup을 더 연구 할 것입니다. – Squiggles

0

유령보기에서 먼저 선형 또는 상대적인 레이아웃을 추가하십시오. 그런 다음 레이아웃 내에 뷰를 추가 할 수만 있다면 XML 파일에 뷰를 추가하기 만하면됩니다.

동적 레이아웃을 만들면 그 레이아웃 안에 뷰만 추가 할 수 있습니다.

RelativeLayout relative= new RelativeLayout(findViewById(R.id.your relativeLayoutID)); 
    relative.addView(child); 

어린이는 버튼 텍스트 뷰 및 위젯 일 수 있습니다.

+0

감사합니다.하지만 특별히 정의 된 레이아웃의 제한을 피하려고합니다. 내가 (절대적으로) 사용할 유일한 것은 더 이상 사용되지 않습니다. – Squiggles

관련 문제