2017-01-24 63 views
0

我使用ViewStubs以編程方式創建一些視圖,如下所示。Android在充氣後查找視圖

RelativeLayout allCommissionsContainer = (RelativeLayout) mBaseLayout.findViewById(R.id.allCommissionsContainer); 

     ViewStub viewStubCommission = new ViewStub(mActivity); 

     viewStubCommission.setLayoutResource(R.layout.viewstub_commission_container); 
     allCommissionsContainer.addView(viewStubCommission); 

     viewStubCommission.inflate(); 

但是,如何在代碼後面找到膨脹視圖(RelativeLayout)?如果我使用viewStubCommission.setTag("MyTag");是否不適用於實際的ViewStub而不是膨脹的視圖?我試過,但它給了我一個null pointer exception。我需要或allCommissionsContainer.findViewWithTag("MyTag")

回答

0

一些測試後,我意識到,我可以用

RelativeLayout myRelativeLayout = (RelativeLayout) viewStubCommission.inflate(); 

,然後設置一個標籤或標識

myRelativeLayout.setTag("MyTag");