LinearLayout layout = setupLayout();
int count = layout.getChildCount();
View v = null;
for(int i=0; i<count; i++) {
v = layout.getChildAt(i);
//do something with your child element
}
ViewGroup layoutCont= (ViewGroup) findViewById(R.id.linearLayout);
getAllChildElements(layoutCont);
public static final void getAllChildElements(ViewGroup layoutCont) {
if (layoutCont == null) return;
final int mCount = layoutCont.getChildCount();
// Loop through all of the children.
for (int i = 0; i < mCount; ++i) {
final View mChild = layoutCont.getChildAt(i);
if (mChild instanceof ViewGroup) {
// Recursively attempt another ViewGroup.
setAppFont((ViewGroup) mChild, mFont);
} else {
// Set the font if it is a TextView.
}
}
}
5 回答
你总是可以这样做:
我认为这可能会有所帮助:findViewWithTag()
将TAG设置为您添加到布局的每个View,然后像使用ID一样通过TAG获取该View
我会避免从视图的子项中静态地抓取元素 . 它现在可能会起作用,但会使代码难以维护,并且很容易在将来的版本中出现问题 . 如上所述,正确的方法是设置标签并通过标签获取视图 .
你可以这样做 .
如果它们都是按钮,否则转换为查看并检查课程