接下来咱们一块儿了解一下最新版本的布局检查器是如何发挥做用的。首先点击窗口的 View 菜单,找到 Tool Window 子菜单,而后选择 Layout Inspector,这样就打开了布局检查器窗口。android
该版本的布局检查器延续了以前版本的功能而且更加多样化。首先,布局检查器能够用两种方式显示 UI 层次结构: 以二维的轮廓格式,或者以一种称为旋转模式 (rotation mode) 的三维视图形式。git
如今你们已经了解了布局检查器的使用方式。那么接下来咱们经过实例来看一下如何使用它来解决应用的问题。这里咱们有一个简单的示例应用,它包含一个 fragment,其中有一些静态文本和一个图片。若是您在阅读文章时想同步进行操做,能够先按照下面步骤操做建立工程。github
当您运行应用的时候,您会看到一个可爱的 android,可是里面少了一些东西: 底部的导航标签。看一下布局文件,咱们能够看到底部的导航视图是存在的,可是屏幕却没有显示它。bash
有多是 navigation host 的尺寸设置错了,咱们尝试把它的高度设置为 'wrap_content':app
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:defaultNavHost="true"
android:layout_weight="9"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_nav_menu" />
</LinearLayout>
复制代码
回到布局检查器,您能够看到 LinearLayout 的尺寸正常了,可是底部的导航栏的位置不对:工具
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:defaultNavHost="true"
android:layout_weight="9"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_nav_menu" />
</LinearLayout>
复制代码
而后获得以下结果:布局
快快尝试一下布局检查器的新特性,而后和咱们分享您的使用体验吧。欢迎你们向咱们反馈问题,或者告诉咱们新的特性需求。google
点击这里查看 Android 官方中文文档 —— 使用布局检查器调试布局 spa