2015-11-05 77 views
0

我想更改「RelativeLayout」的內容,問題是該佈局的內容在另一個XML中。從另一個XML加載佈局

setContentView(R.layout.activity_fullscreen); 

//from actual xml 
RelativeLayout rL1= (RelativeLayout) findViewById(R.id.principalLayout); 

//from another xml 
RelativeLayout rL2= (RelativeLayout) findViewById(R.id.cocinaL); 

rL1.removeAllViews(); 
rL1.addView(rL2); //this fail because rL2 is null 

感謝

回答

1

你不能沒有LayoutInflater

setContentView(R.layout.activity_fullscreen); 


    //your actule view 
    RelativeLayout rL1= (RelativeLayout) findViewById(R.id.principalLayout); 

    LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    // your second xml 
    View view2 = inflater.inflate(R.layout.myview12, null); 

    // add second View 
    rL1.addView(view2); 
0

//從實際的XML從另一個XML

RelativeLayout rL1= (RelativeLayout) findViewById(R.id.principalLayout); 

//添加另一個佈局查看

View child = getLayoutInflater().inflate(R.layout.child, null); 
RelativeLayout rL2 = (RelativeLayout) child.findViewById(R.id.cocinaL); 
rL1.removeAllViews(); 
rL1.addView(rL2);