2012-05-03 6 views
2

버튼 클릭을 기반으로 런타임에 '템플릿'레이아웃을 런타임에 동적으로 추가해야합니다.런타임에 레이아웃을 레이아웃에 동적으로 삽입하십시오.

다음 코드 블록에는 내 레이아웃의 단순화 된 버전이 나와 있습니다. HEADER C의 내용 (즉, 본문 C1, 본문 C2 ... 본문 Cn)은 런타임에 추가해야하는 구성 요소입니다. 그것들은 모두 같은 형식이며 상대적 레이아웃을 가진 별도의 xml 파일에 정의됩니다. 이 작업을 어떻게 수행합니까? LayoutInflater를 시도했지만 성공하지 못했습니다. LayoutInflater는 팽창시키기 위해 LinearLayout을 필요로합니까?

<SCROLLVIEW> 
    <RELATIVE_LAYOUT> 
     ____________ 
     HEADER A 
     ____________ 
     BODY A 
    </RELATIVE_LAYOUT> 
    <RELATIVE_LAYOUT> 
     ____________ 
     HEADER B 
     ____________ 
    BODY B 
</RELATIVE_LAYOUT> 
<RELATIVE_LAYOUT> 
____________ 
HEADER C 
    ____________ 
    BODY C1 
    ____________ 
    BODY C2 
    . 
    . 
    . 
    ____________ 
    BODY Cn 
<RELATIVE_LAYOUT> 
</SCROLLVIEW> 

감사합니다. 모든 안내에 감사드립니다.

+0

붙여 넣기 코드 :에 위의 내용을 변경합니다. – jeet

답변

1
LinearLayout Parent = (LinearLayout) findViewById(R.id.linearLayout); 

    View child = getLayoutInflater().inflate(R.layout.main_new,null); 
    Parent.addView(child); 
1

사용

View headerView = View.inflate(this, R.layout.class_name, null); 

는 레이아웃을 팽창합니다.

1
/**rootView represent the RelativeLayout*/ 
View headerView = LayoutInflater.from(context).inflater(R.layout.header_view, rootView, false); 
rootView.add(headerView); 
1

당신은 너무 같은 것을 수행 할 수

general = (LinearLayout) findViewById(R.id.general_tab); // get the id of the layout you wish to add your view to 
TextView programs = new TextView(this); // create a new view/widget to add 
programs.setText("Program(s): " + prgdisp); // set the text 
general.addView(programs); // add the new view/widget to the existing layout 

편집

난 당신이 이미 그것을위한 XML 레이아웃을 한 것으로 놓쳤다. 팽창 여기에 레이아웃을 추가

general = (LinearLayout) findViewById(R.id.general_tab); // get the id of the layout you wish to add your view to 
View header = View.inflate(this, R.layout.class_name, null); // inflate your layout 
general.addView(header); // add the new view/widget to the existing layout 
관련 문제