Cod*_*ody 54 java android android-linearlayout
有没有办法获取LinearLayout的子元素?我的代码返回一个视图(linearlayout),但我需要访问布局中的特定元素.
有什么建议?
(是的,我知道我可以使用findViewById,但我在java中创建布局/子项 - 而不是XML.)
Ale*_*s G 83
你总是可以这样做:
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
}
Run Code Online (Sandbox Code Playgroud)
LinearLayout layout = (LinearLayout)findViewById([whatever]);
for(int i=0;i<layout.getChildCount();i++)
{
Button b = (Button)layout.getChildAt(i)
}
Run Code Online (Sandbox Code Playgroud)
如果它们都是按钮,否则强制转换以查看和检查类
View v = (View)layout.getChildAt(i);
if (v instanceof Button) {
Button b = (Button) v;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
57578 次 |
最近记录: |