如何计算布局中的视图量

时间:2015-08-12 13:58:29

标签: android xml android-layout android-studio android-view

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

2 个答案:

答案 0 :(得分:2)

你试过这个吗?:

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

    }

答案 1 :(得分:0)

对于这个问题的新来者,你可以使用android studio的android设备监视器中的视图层次结构:

从您的设备或模拟器中打开应用程序,然后浏览到要检查的视图

从Studio的菜单栏中打开

1-

 tools -> Android -> Android device monitor

2-从Android设备监视器菜单栏打开

 window -> open perspective -> Hierarchy view

它可能会加载一段时间。

如果您想了解更多信息:https://developer.android.com/studio/profile/hierarchy-viewer.html