首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >混淆findViewById()

混淆findViewById()
EN

Stack Overflow用户
提问于 2015-11-10 23:16:34
回答 2查看 65关注 0票数 0

我正在尝试以编程方式将LinearLayouts添加到现有的RelativeLayout中。每个LinearLayout将包含一些按钮,我希望能够通过设置容器LinearLayout的可见性来切换每组按钮的可见性。

代码语言:javascript
复制
// 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);

当我以后尝试检索这些布局时,我的问题就出现了,例如,为了能够打开/关闭它们的可见性。我想我可以用

代码语言:javascript
复制
LinearLayout ll = (LinearLayout) findViewById(layoutIndex);

但是findViewById()给了我一个错误,当我试图提供一个int时,它需要一个资源ID。有什么简单的方法可以将我为这些布局分配的ID转换为R.id.XXX ID吗?

谢谢你,安迪

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-10 23:25:06

findViewById(id)查找作为定义布局的XML的一部分而包含的元素。

您可能对getChildAt(index)有更好的运气,它在传递的索引处返回视图。

票数 1
EN

Stack Overflow用户

发布于 2015-11-10 23:24:04

是!在不使用ID的容器中查找所有LinearLayout

代码语言:javascript
复制
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
     }
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33641502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档