2017-07-02 103 views
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即可。

相關問題