如何计算布局中的视图数量

Mac*_*ver 5 xml android android-layout android-view android-studio

Android Studio 有没有办法显示 XML 布局中存在的视图数量?众所周知,布局应该包含 <=80 个视图,因此超过这个数量就会出现此警告,因此告知数量将非常有帮助。

LayoutName.xml 有 80 多个视图,对性能不利

public class FragmentApple extends android.support.v4.app.Fragment {

    public FragmentApple () {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_apple,container,false);

        int num = FragmentApple.getChildCount(); Log.d("Number of views in layout: ", "" + num);

        return v;
    }
}
Run Code Online (Sandbox Code Playgroud)

eya*_*nna 6

你试过这个吗?:

layout.getChildCount();
Run Code Online (Sandbox Code Playgroud)

更新

在我们在评论中讨论之后,这是您应该在代码中执行的操作:

public class FragmentApple extends android.support.v4.app.Fragment {

public FragmentApple () {
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_apple,container,false);



    return v;
}

@Override
public void onStart(){
super.onStart()
RelativeLayout (or Linear as in your xml) rl = (RelativeLayout) v.findViewbyId(R.id.your_layout_id)

int num = rl.getChildCount();
 Log.d("Number of views in layout: ", "" + num);
}

    }
Run Code Online (Sandbox Code Playgroud)