Is there an Android tool to find the name of a layout for a running app?

后端 未结 7 1585
刺人心
刺人心 2021-02-07 03:37

Background

I have been recently hired to maintain a very large program (only two Activities, about a hundred Fragments, and several hundred layouts).

7条回答
  •  温柔的废话
    2021-02-07 04:03

    Hierarchy Viewer tool

    In the Android Device Monitor, there is a button you can click on called "Dump View Hierarchy for UI Automator".

    This will open a UI Hierarchy viewer in the Device Monitor, which you can use to see the resource IDs of each of your views.

    While this does not actually give you the name of the inflated XML file, it could be very useful. If you are lucky, you can even grep for the resource IDs to narrow down the search for which XML files to look at.

    And if you hover over the title of the viewer window, it will show you the path to the actual XML file that it created. This is a way to find all the resource IDs in one place.


    Some more info on Hierarchy Viewer in case it helps:

    • How to get to Hierarchy View while running the app on my device connected to android studio
    • or straight from the Android Developer docs

    The Hierarchy Viewer allows you to debug and optimize your user interface. It provides a visual representation of the layout's View hierarchy (the Layout View) and a magnified inspector of the display (the Pixel Perfect View).


    Create a custom Layout Inflater in code

    Here's a blog post that contains something interesting - a custom layout inflater that intercepts all view inflation:

    • http://android-activities.blogspot.co.za/2014/03/add-custom-behavior-to-standard-android.html

    It may be possible to use that sample code to intercept the inflate calls, and receive the resource ID of the XML file:

    • LayoutInflator.inflate() on Android Developer docs

    At this point, you will need to turn the resource id into a useful name. Quoting from this question (How to get Resource Name from Resource id), you can use:

    getResources().getResourceEntryName(int resid);
    

    Add some logging and this may give you each XML file as it is being inflated.

    This could be quite a nice way to document your entire project.

提交回复
热议问题