Android 计算布局中的可见元素

Android 计算布局中的可见元素,android,android-linearlayout,Android,Android Linearlayout,假设在我的LinearLayout(比如parentLayout)中有5个其他LinearLayout(比如childLayout),其中目前只有一个可见。其他布局依赖于某些外部事件使其可见。如何计算父布局中可见的子布局数?您可以迭代父布局的子布局并检查其可见性。大概是这样的: LinearLaout parent = ...; int childCount = parent.getChildCount(); int count = 0; for(int i = 0; i < childC

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

您可以迭代父布局的子布局并检查其可见性。大概是这样的:

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);
LinearLaout parent=。。。;
int childCount=parent.getChildCount();
整数计数=0;
for(int i=0;i
这里有一个函数,返回视图组中可见的子对象数,如LinearLayout、RelativeLayout、ScrollView等

private int countVisible(ViewGroup myLayout)
{
    if(myLayout==null) return 0;
    int count = 0;
    for(int i=0;i<myLayout.getChildCount();i++)
    {
        if(myLayout.getChildAt(i).getVisibility()==View.VISIBLE)
            count++;
    }
    return count;
}
private int countVisible(视图组myLayout)
{
if(myLayout==null)返回0;
整数计数=0;
对于(int i=0;i