如何从Java inflate中获取布局id
我是Android编程新手。。。 我想通过编程将布局添加到如何从Java inflate中获取布局id,java,android,xml,Java,Android,Xml,我是Android编程新手。。。 我想通过编程将布局添加到滚动视图,因此我使用下面的代码 for(int i = 0; i < 10; i++){ LinearLayout myLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.my_layout, null); ImageView imgView = (ImageView) getLayoutInflater().inflate(R.layout.im
滚动视图
,因此我使用下面的代码
for(int i = 0; i < 10; i++){
LinearLayout myLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.my_layout, null);
ImageView imgView = (ImageView) getLayoutInflater().inflate(R.layout.img_view, null);
//set ImageView source
TextView txtView = (TextView) getLayoutInflater().inflate(R.layout.txt_view, null);
//set TextView text
myLayout.addView(imgView);
myLayout.addView(txtView);
scrllView.addView(myLayout);
}
然后我可以在我的Java代码中膨胀my_layout.xml
文件,使用它们的id
LinearLayout myLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.my_layout, null);
ImageView imgView = //find the child of myLayout (ImageView) with ID of img
//set ImageView source
TextView txtView = //find the child of myLayout (TextView) with ID of txt
//set TextView text
我想知道是否有可能做到这一点。好的。。。我终于得到了我想要的。下面的代码工作得很好
LinearLayout myLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.my_layout, null);
ImageView imgView = myLayout.findViewById(R.id.img);
//set ImageView source
TextView txtView = myLayout.findViewById(R.id.txt);
//set TextView text
LinearLayout myLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.my_layout, null);
ImageView imgView = myLayout.findViewById(R.id.img);
//set ImageView source
TextView txtView = myLayout.findViewById(R.id.txt);
//set TextView text