如何获取包括子视图在内的布局中的视图总数

How to get the total number of Views in a lyout including subviews

我正在尝试计算向用户显示的任何布局中的所有视图。为了实现它,使用了下面发布的代码,但正如您在布局中看到的那样,它包含

    Button
    LinearLayout
        Button
        Button
    TextView
    LinearLayout

执行代码时,它说布局只有 4 个小部件 "Button, LinearLayout, TextView, LinearLayout",而我预计代码为 return 6 个小部件。 您能告诉我如何获取包含 sub-widgets/views 的布局中包含的总小部件吗?

代码:

public class ActMain extends AppCompatActivity {

private static final String TAG = ActMain.class.getSimpleName();
private LinearLayout mMainContainer = null;

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

    this.mMainContainer = (LinearLayout) findViewById(R.id.actMain_mainContainer);
    int totalWidgets = this.mMainContainer.getChildCount();
    Log.d(TAG, "totalWidgets:" + totalWidgets);


    for (int i = 0; i < this.mMainContainer.getChildCount(); i++) {
        View view = mMainContainer.getChildAt(i);
    }
}

}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/actMain_mainContainer"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.alten.test_traversethroughaview_1.ActMain">

    <Button
        android:id="@+id/actMain_btn_change"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/decision_change" />

        <LinearLayout
            android:id="@+id/actMain_linlay_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/actMain_btn_yes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/decision_yes" />
            <Button
                android:id="@+id/actMain_btn_no"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/decision_no" />
        </LinearLayout>

        <TextView
            android:id="@+id/actMain_tv_display"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:id="@+id/actMain_linlay_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        </LinearLayout>

您的代码只计算其 parent child,而不是布局中的所有 child。这意味着您计算的是祖父的 child 而不是祖父的祖父 child。您必须遍历所有视图才能计算所有 child。为此,您可以使用递归函数来计算所有 child.

getChildCount(View)
  count = 1;
  check the view is instance of ViewGroup
  if it is ViewGroup 
    for each child 
      count+= getChildCount(child)

  return count

用根布局调用这个

totalChild = getChildCount(root)

希望你能把所有的child数都这样。

这个函数可能会有所帮助:

private int countChild(View view) {
    if (!(view instanceof ViewGroup))
        return 1;

    int counter = 0;

    ViewGroup viewGroup = (ViewGroup) view;

    for (int i=0; i<viewGroup.getChildCount(); i++) {
        counter += countChild(viewGroup.getChildAt(i));
    }

    return counter;
}

它只计算叶子(忽略 ViewGroups)。但是,如果您还想计算它们,如果很简单:只需将 counter = 0 替换为 counter = 1.

使用这个方法:

public int getChildrenViews(ViewGroup parent){
    int count = parent.getChildCount();
    for (int i=0;i<parent.getChildCount();i++){
        if (parent.getChildAt(i) instanceof ViewGroup){
            count+=getChildrenViews((ViewGroup) parent.getChildAt(i));
        }
    }
    return count;
}

任何你想添加这部分的地方:

if (getWindow().getDecorView().getRootView() instanceof ViewGroup){
        getChildrenViews((ViewGroup) getWindow().getDecorView().getRootView());
    }