简体   繁体   English

获取所有linearlayout子元素

[英]Get all linearlayout child elements

I have layout: 我有布局:

 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/exchangeView"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#fff000" android:gravity="center" android:text="Sticker 1" android:textColor="#372c24" android:textSize="20dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom" android:layout_alignParentBottom="true" android:orientation="horizontal" android:id="@+id/next"> <Button android:id="@+id/btn_next" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_alignParentBottom="true" android:text="Next" /> </LinearLayout> </LinearLayout> </ScrollView> 

and related code: 及相关代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.exchange);


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

next thing I want to do is to get all children elements in this layout with Id=exchangeView , but when I type layout.getChildCount() android studio IDE compiler says: 接下来要做的是使用Id=exchangeView获取此布局中的所有子元素,但是当我键入layout.getChildCount() android studio IDE编译器会说:

Cannot resolve method 'getChildCount()' 无法解析方法“ getChildCount()”

However, if I put breakpoint on line where I assign layout a value, then if I evaluate expression I can execute method layout.getChildCount() 但是,如果将断点放在分配layout值的行上,那么如果我评估表达式,则可以执行layout.getChildCount()方法。

在此处输入图片说明

All I want to do is to execute layout.getChildCount() during runtime. 我要做的就是在运行时执行layout.getChildCount()

Thanks in advance! 提前致谢!

You can try to use getChildCount() and also getChildAt() like this may be 您可以尝试使用getChildCount()和getChildAt(),例如

int childcount = ll.getChildCount();
for (int i=0; i < childcount; i++){
  View v = ll.getChildAt(i);
}

Change this line 更改此行

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

to

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

I am assuming you have declared the variable layout as 我假设您已将变量布局声明为

View layout;

If so, change that to 如果是这样,将其更改为

LinearLayout layout;

The View class doesn't have the method getChildCount(), it is a method in the ViewGroup class. View类没有方法getChildCount(),它是ViewGroup类中的方法。 LinearLayout extends ViewGroup, and hence has access to it. LinearLayout扩展了ViewGroup,因此可以访问它。 View does not. 视图不。 This is related to polymorphism, which I suggest you look up. 这与多态有关,我建议您查一下。 Cheers. 干杯。

Try this: 尝试这个:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM