接下來咱們一塊兒瞭解一下最新版本的佈局檢查器是如何發揮做用的。首先點擊窗口的 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