Android 获取所有linearlayout子元素

Android 获取所有linearlayout子元素,android,android-layout,Android,Android Layout,我有一个布局: 试试这个: 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 } 试试这个: LinearLayout layout = setupLayout

我有一个布局:

试试这个:

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
}
试试这个:

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
}
您可以尝试使用getChildCount和getChildAt,就像这样

您可以尝试使用getChildCount和getChildAt,就像这样

换行

layout = (View) findViewById(R.id.exchangeView);

我假设您已将变量布局声明为

View layout;
如果是,请将其更改为

LinearLayout layout;
View类没有方法getChildCount,它是ViewGroup类中的一个方法。LinearLayout扩展了视图组,因此可以访问它。视图没有。这与多态性有关,我建议你查一下。 干杯。

换一行

layout = (View) findViewById(R.id.exchangeView);

我假设您已将变量布局声明为

View layout;
如果是,请将其更改为

LinearLayout layout;
View类没有方法getChildCount,它是ViewGroup类中的一个方法。LinearLayout扩展了视图组,因此可以访问它。视图没有。这与多态性有关,我建议你查一下。
干杯。

是的,这就是我的问题所在。谢谢!是的,这就是我的问题所在。谢谢!