计算布局中的可见元素

Rak*_*ari 6 android android-linearlayout

假设在我的LinearLayout(比如parentLayout)中有另外5个LinearLayouts(比如childLayout),其中只有其中一个可见.其他布局依赖于某些外部事件以使其可见.如何计算parentLayout中可见的childLayout数?

Ale*_*s G 10

您可以迭代父布局的子项并检查它们的可见性.像这样的东西:

LinearLaout parent = ...;
int childCount = parent.getChildCount();
int count = 0;
for(int i = 0; i < childCount; i++) {
    if(parent.getChildAt(i).getVisibility() == View.VISIBLE) {
        count++;
    }
}
System.out.println("Visible children: " + count);
Run Code Online (Sandbox Code Playgroud)