Android 获取子视图的计数?

Android 获取子视图的计数?,android,Android,我有一个LinearLayout,其中包括x个LinearLayout(子项)和一组其他视图,如文本视图、相对视图等 如何仅返回LinearLayout视图中的子项数量?通常您会使用ViewGroup.getChildCount(),但由于您只需要LinearLayout的计数,因此需要创建一个算法来执行此操作: public int linearLayoutChildCount(ViewGroup parent) { int count = 0; for (int x = 0;

我有一个LinearLayout,其中包括x个LinearLayout(子项)和一组其他视图,如文本视图、相对视图等


如何仅返回LinearLayout视图中的子项数量?

通常您会使用
ViewGroup.getChildCount()
,但由于您只需要LinearLayout的计数,因此需要创建一个算法来执行此操作:

public int linearLayoutChildCount(ViewGroup parent) {
    int count = 0;
    for (int x = 0; x < parent.getChildCount(); x++) {
        if (parent.getChildAt(x) instanceof LinearLayout) {
            count++;
        }
    }
    return count;
}
public int linearLayoutChildCount(视图组父级){
整数计数=0;
对于(int x=0;x

当然,这样做并不理想,因为它使用的反射在Android上可能会很慢。

通常您会使用
ViewGroup.getChildCount()
,但由于您只需要线性布局的计数,因此需要创建一个算法来执行此操作:

public int linearLayoutChildCount(ViewGroup parent) {
    int count = 0;
    for (int x = 0; x < parent.getChildCount(); x++) {
        if (parent.getChildAt(x) instanceof LinearLayout) {
            count++;
        }
    }
    return count;
}
public int linearLayoutChildCount(视图组父级){
整数计数=0;
对于(int x=0;x

当然,这样做并不理想,因为它使用的反射在Android上可能会很慢。

通常您会使用
ViewGroup.getChildCount()
,但由于您只需要线性布局的计数,因此需要创建一个算法来执行此操作:

public int linearLayoutChildCount(ViewGroup parent) {
    int count = 0;
    for (int x = 0; x < parent.getChildCount(); x++) {
        if (parent.getChildAt(x) instanceof LinearLayout) {
            count++;
        }
    }
    return count;
}
public int linearLayoutChildCount(视图组父级){
整数计数=0;
对于(int x=0;x

当然,这样做并不理想,因为它使用的反射在Android上可能会很慢。

通常您会使用
ViewGroup.getChildCount()
,但由于您只需要线性布局的计数,因此需要创建一个算法来执行此操作:

public int linearLayoutChildCount(ViewGroup parent) {
    int count = 0;
    for (int x = 0; x < parent.getChildCount(); x++) {
        if (parent.getChildAt(x) instanceof LinearLayout) {
            count++;
        }
    }
    return count;
}
public int linearLayoutChildCount(视图组父级){
整数计数=0;
对于(int x=0;x

当然,这样做并不理想,因为它使用的反射在Android上可能会很慢。

使用getChildCount,遍历它,然后使用LinearLayout的视图实例只获取LinearLayout的子项使用getChildCount,迭代并使用LinearLayout的view instanceof LinearLayout仅获取LinearLayout的子项使用getChildCount,迭代并使用LinearLayout的view instanceof LinearLayout仅获取LinearLayout的子项使用getChildCount,迭代该过程,并使用LinearLayout的视图实例来获取LinearLayout@andrew
instanceof
是我指的反射,因为您正在运行时确定对象的类型。@andrew
instanceof
是我指的反射,因为您在运行时确定对象的类型。@andrew
instanceof
是我指的反射,因为您在运行时确定对象的类型。@andrew
instanceof
是我指的反射,因为您在运行时确定对象的类型。