A new Layout Inspector was introduced in Android Studio 4.0. It is similar to Chrome DevTools — we can inspect views with IDs, a new 3D view makes it easy to see view hierarchy, and most of all, we can navigate to the exact line of a view’s code from the inspector.

Android Studio 4.0中引入了新的布局检查器。 它类似于Chrome DevTools-我们可以检查具有ID的视图,新的3D视图可以轻松查看视图层次结构,最重要的是,我们可以从检查器导航到视图代码的确切行。

Debugging UI issues has been an issue for Android developers since the beginning. With the Layout Inspector, it’s now easy to find the view hierarchy and pinpoint the issues. We can use the inspector with both physical devices and emulators.

从一开始,调试UI问题就一直是Android开发人员面临的问题。 借助Layout Inspector,现在可以轻松找到视图层次结构并查明问题。 我们可以将检查器与物理设备和仿真器一起使用。

To start the Layout Inspector navigate, go to View > Tools Window > Layout Inspector.

要启动“布局检查器”导航,请转到“视图”>“工具窗口”>“布局检查器”。

Now, a new window with the Layout Inspector opens at the bottom of the studio. By default, you can see the current screen of the emulator/device in the middle. On the left side, there are layout-related things like the view hierarchy. On the right side of the window, it shows the information about the view that is currently selected. Have a look:

现在,在工作室的底部将打开一个带有“布局检查器”的新窗口。 默认情况下,您可以在中间看到仿真器/设备的当前屏幕。 左侧是与布局相关的内容,例如视图层次结构。 在窗口的右侧,它显示有关当前所选视图的信息。 看一看:





Navigating to Layout Inspector 导航到布局检查器

With that said, let’s start digging into the features.

话虽如此,让我们开始深入研究功能。

(Live Updates)

By default, you can inspect the screen that is currently active when you launch the Layout Inspector. But if you want to inspect all the layouts that you can navigate on an emulator/device, then you have to enable the “Live updates” option that is present at the top of the window. Have a look:

默认情况下,您可以在启动Layout Inspector时检查当前处于活动状态的屏幕。 但是,如果要检查可在模拟器/设备上浏览的所有布局,则必须启用窗口顶部显示的“实时更新”选项。 看一看:


Live updates

实时更新

(3D Rotation)


3D rotation of the layout

布局的3D旋转

3D rotation is a new feature. As the name indicates, we can see the 3D view of the layout. To be more precise, we can see the view hierarchy. By default, the Layout Inspector launches in 2D mode, and to switch between 2D and 3D, we need to use a rotation icon beside the layout screen at the bottom.

3D旋转是一项新功能。 顾名思义,我们可以看到布局的3D视图。 更确切地说,我们可以看到视图层次结构。 默认情况下,布局检查器以2D模式启动,要在2D和3D之间切换,我们需要在底部的布局屏幕旁边使用旋转图标。

With this 3D rotation, we can see how the view is laid out. The 3D rotation feature is only available on devices API level 29 and above.

通过3D旋转,我们可以看到视图的布局方式。 3D旋转功能仅在设备API级别29及更高版本上可用。

(Hide Views)


Changing the visibility of the views 更改视图的可见性


Hiding views is another cool feature in the new version of the Layout Inspector. We can hide subviews of a view by right-clicking on the view and selecting the “Show Only Parents” option. Similarly, we can also hide the parent of a view with the “Show Only Subtree” option. Finally, to reset or to show all the views, we can use the “Show All” option.

隐藏视图是新版本的Layout Inspector中的另一个很酷的功能。 我们可以通过右键单击视图并选择“仅显示父级”选项来隐藏视图的子视图。 同样,我们也可以使用“仅显示子树”选项隐藏视图的父级。 最后,要重置或显示所有视图,我们可以使用“显示全部” 选项。

This feature comes in handy when you’re specifically dealing with a particular part of the layout and you don’t want to get distracted by the entire layout.

当您专门处理布局的特定部分并且不想被整个布局分散注意力时,此功能会派上用场。

(Navigation to Code)

This is one of my favorite features in the new version. When we click on a view, a new panel will be opened on the right side of the window. This panel contains all the information about the view that was selected by the developer. This information includes all the attributes of the view that you used in the actual layout and additional information like the X and Y coordinates of the view, actual height, and width.

这是新版本中我最喜欢的功能之一。 当我们单击一个视图时,将在窗口的右侧打开一个新面板。 该面板包含有关开发人员选择的视图的所有信息。 此信息包括您在实际布局中使用的视图的所有属性,以及其他信息,例如视图的X和Y坐标,实际高度和宽度。

That’s not all. The Layout Inspector will show navigation links under the attributes that were used in the actual layout so that you can navigate directly to that line of code and make changes if necessary. This feature helps us to concentrate on what matters. Have a look:

那不是全部。 布局检查器将在实际布局中使用的属性下显示导航链接,以便您可以直接导航到该行代码,并在必要时进行更改。 此功能可帮助我们专注于重要的事情。 看一看:


(Search)


Searching textview with ID and content (hello world) 搜索带有ID和内容的文本视图(Hello World)

When a view is inflated on the Layout Inspector, it shows all the attributes — including the ones that we didn’t use in the actual layout — and you can see them on the right panel. Finding what you’re looking for might get tricky in situations like this, and that’s when the search feature comes in handy.

在“布局”检查器中放大视图时,它会显示所有属性(包括我们在实际布局中未使用的属性),您可以在右侧面板上看到它们。 在这种情况下,找到您要寻找的东西可能会很棘手,这就是搜索功能派上用场的时候。

We can search for any attribute with the name or content of the view, as shown in the GIF above.

我们可以搜索带有视图名称或内容的任何属性,如上面的GIF所示。

(Bonus)

To learn more about Android Studio and debugging, read the following articles:

要了解有关Android Studio和调试的更多信息,请阅读以下文章:

Thank you for reading.

感谢您的阅读。


翻译自: https://medium.com/better-programming/explore-the-new-layout-inspector-in-android-studio-4-0-6f9ffb1505f2