2014-09-08 2 views
-2

동일한보기 내에서 기존 정적 버튼을 클릭하여 뷰에 새 버튼을 추가하고 싶습니다. 그리고 버튼은 우리가 수동으로 삭제할 때까지보기에 있어야합니다.android에서 기존 버튼을 클릭하여 같은 레이아웃 내에서 버튼을 동적으로 생성하십시오.

잠시 동안이 문제에 대한 도움을 얻는다면 동적 버튼을 만드는 것이 가능합니다. 희망을 얻으려는 사람이 나를 도울 수 있기를 바랍니다.

+0

버튼을 동적으로 만들고 필요에 따라 layoutparams를 프로그래밍 방식으로 설정하십시오. – praveen

답변

0

당신은 findView 레이아웃과 활동에서 onCreate 함수에 위치한이 코드처럼 그에 버튼을 추가하려면 액세스해야합니다 :이 전화를 기존의 버튼 클릭 리스너에

Button staticBtn = (Button)(findViewById(R.id.staticBtnID)); 
LayoutParam staticBtnLayoutParam = staticBtn.getLayoutParam(); 
Button btn = new Button(this); 
int width = 0; //you can use staticBtnLayoutParam.getWidth(); or .getX(); to get staticBtn parameters and set your param related to that 
int height = 0 ; 
btn.setLayoutParams(new LayoutParams(width , height)); 
LinearLayout layout = (LinearLayout)(findViewById(R.id.linearLayoutID)); 
layout.addView(button); 

안부

1

을 기능.

private void addButton(){ 
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout);//create a layout in you content view 
layout.setOrientation(LinearLayout.VERTICAL); 

    Button newButton = new Button(this); 
    newButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
    LayoutParams.WRAP_CONTENT)); 

    newButton.setText("Button Text"); 
    newButton.setId(1);//some id 
    layout.addView(row); // add button in layout 
} 
관련 문제