Google Map Activity启动后Toolbar立即消失的排查求助
解决Toolbar切换后启动即消失的问题
嘿,我仔细看了你贴的代码,发现Toolbar消失的核心原因是布局层级和约束设置错误,Google Map Fragment直接把Toolbar给盖住了,另外还有几个小细节可以优化,咱们一步步来改:
第一步:修复activity_map.xml的布局约束
你在ConstraintLayout里引入Toolbar的时候,没给它设置任何约束,而Map Fragment是match_parent占满整个屏幕,结果Toolbar就被压在Fragment下面看不见了。修改后的activity_map.xml应该是这样:
<android.support.v4.widget.DrawerLayout 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/nav_drawer" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 给Toolbar设置约束,固定在屏幕顶部 --> <include layout="@layout/toolbar" android:id="@+id/include_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <!-- 让Map Fragment在Toolbar下方,占满剩余的屏幕空间 --> <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/include_toolbar" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" tools:context="com.christopher_sheridan.wheres_my_ride.MapActivity"/> </android.support.constraint.ConstraintLayout> <!-- 把NavigationView补全,确保它在DrawerLayout的子层级里 --> <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:menu="@menu/your_nav_menu"/> </android.support.v4.widget.DrawerLayout>
第二步:简化toolbar.xml的布局
你toolbar.xml外层的LinearLayout完全是多余的,直接用AppBarLayout包裹Toolbar就行,减少不必要的布局层级:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/minimalist_grey" app:popupTheme="@style/AppTheme.PopupOverlay"> <TextView android:id="@+id/toolbarDistanceText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="start|center_vertical" android:textSize="20sp" android:text="Distance (Just for testing)" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_share_white" android:background="@android:color/transparent" android:layout_gravity="end" android:layout_marginEnd="20dp"/> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout>
第三步:优化代码和主题的小细节
- 你代码里的
if(getSupportActionBar() != null) { getSupportActionBar().setDisplayShowTitleEnabled(false); }可以删掉了,因为你已经用自定义的Toolbar替换了系统ActionBar,这行代码现在没啥用。 - 关于调试:以后遇到这种布局消失的问题,先去Android Studio的布局预览里看看activity_map.xml的样子,一眼就能发现覆盖问题;如果要打代码断点,就在
setSupportActionBar(toolbar);这一行打,看看toolbar对象是不是null,或者有没有成功设置为ActionBar。
做完这些修改后,Toolbar应该就能正常显示在Map上方了,不会再启动就消失啦。
内容的提问来源于stack exchange,提问作者DevOpsSauce
相关产品推荐
相关产品推荐

