gpt4 book ai didi

android - 如何计算布局中的 View 数量

转载 作者:行者123 更新时间:2023-12-03 17:46:52 32 4
gpt4 key购买 nike

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

LayoutName.xml has more than 80 views, bad for performance


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;
}
}

最佳答案

你试过这个吗?:

layout.getChildCount();

更新

在我们在评论中讨论之后,这是您应该在代码中执行的操作:
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);
}

}

关于android - 如何计算布局中的 View 数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31967380/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com