2014-02-12 74 views
-1

如何使用同一佈局上的按鈕將更多項目添加到活動佈局。項目包括像TextViews,EditViews等項目,這些項目需要添加到按鈕所在的同一個佈局中。請幫助我。考慮一下Android開發中的noob。按鈕添加項目的操作

回答

0

由於您試圖通過按鈕添加項目,因此必須以編程方式添加組件。

這裏有一個如何編程通過點擊一個按鈕來添加一個按鈕一個例子:

final Activity act = this; 
//the layout on which you are working 
//if using LinearLayout cast it correctly 
final RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout_general); 

Button btnAdd = (Button)findViewById(R.id.btn_add); 

btnAdd.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 


     //set the properties for button 
     Button btnTag = new Button(act); 
     //if using LinearLayout change to LinearLayout.LayoutParams 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT,  
       LayoutParams.WRAP_CONTENT 
     ); 
     //Set place in layout, make sure you change the values everytime you insert 
     //another item otherwise the items will be placed on top of each other 
     params.setMargins(200, 40, 100, 50); 
     btnTag.setLayoutParams(params); 
     btnTag.setText("Button"); 

     //add button to the layout 
     layout.addView(btnTag); 

    } 
}); 

這個片段添加到您的onCreate(),並作出適當的名稱的變化,你是好去。

希望它有幫助。