我正在尝试以编程方式将LinearLayouts添加到现有的RelativeLayout中。每个LinearLayout将包含一些按钮,我希望能够通过设置容器LinearLayout的可见性来切换每组按钮的可见性。
// We iterate over a list and call the following to create the new
// layout assigning an index from a int counter
LinearLayout LL = new LinearLayout(MainActivity.this);
LL.setOrientation(LinearLayout.VERTICAL);
LL.setId(nextId);
LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
LL.setWeightSum(6f);
LL.setLayoutParams(LLParams);
LinearLayout mll=((LinearLayout) findViewById(R.id.menuLayout));
mll.addView(LL);当我以后尝试检索这些布局时,我的问题就出现了,例如,为了能够打开/关闭它们的可见性。我想我可以用
LinearLayout ll = (LinearLayout) findViewById(layoutIndex);但是findViewById()给了我一个错误,当我试图提供一个int时,它需要一个资源ID。有什么简单的方法可以将我为这些布局分配的ID转换为R.id.XXX ID吗?
谢谢你,安迪
发布于 2015-11-10 23:25:06
findViewById(id)查找作为定义布局的XML的一部分而包含的元素。
您可能对getChildAt(index)有更好的运气,它在传递的索引处返回视图。
发布于 2015-11-10 23:24:04
是!在不使用ID的容器中查找所有LinearLayout。
LinearLayout mll= (LinearLayout) findViewById(R.id.menuLayout);//container
for(int i=0; i<mll.getChildCount(); ++i) {
View nextChild = mll.getChildAt(i);
if (nextChild instanceof LinearLayout ) {
//TODO add your code here nextChild is a LL that you wanna find
}
}https://stackoverflow.com/questions/33641502
复制相似问题