1

我正在尝试计算显示给用户的任何布局中的所有视图。为了实现它,贴在下面的代码中使用,但你在布局看,它包含如何获取包含子视图在内的所有视图总数

Button 
    LinearLayout 
     Button 
     Button 
    TextView 
    LinearLayout 

代码被执行时,它说,布局只有4个组件“按钮,LinearLayout中,TextView的, LinearLayout“,而我期望代码返回6个小部件。 你能告诉我如何让包含子小部件/视图的布局中包含的小部件总数?

代码

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> 

回答

0

使用这种方法:

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()); 
    } 
+0

谢谢你的塔答案。但有没有另一种方式来当前显示在屏幕上的主viewGroup /主容器? – LetsamrIt

+0

@LetsamrIt 使用此行访问当前屏幕中的主要布局: getWindow()。getDecorView()。getRootView(); ()ViewGroup)getChildrenViews((ViewGroup)getWindow()。getDecorView()。getRootView());如果使用getWindow()方法,则返回true;如果getWindow()方法不能使用, }' 我改变了主要答案。 –

0

你的代码只计算其父母的孩子,不是所有的孩子在外行出。这意味着你正在计算祖父的孩子而不是祖父的孙子。你必须遍历所有视图来统计所有的孩子。要做到这一点,你可以使用递归函数来计算所有的孩子。

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) 

希望你可以用这种方式计算所有的孩子。

0

该功能可能会有所帮助:

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即可。

相关问题