启动Activity后,工具栏立即消失

时间:2018-01-13 20:04:55

标签: android android-toolbar

我最初在Google Map Activity上面有一个ActionBar,但我需要在其中添加多个元素,因此我建议使用工具栏。

好吧,我已成功安装它,但工具栏在启动后立即消失。

我不明白为什么会发生这种情况,因为没有错误,我不知道在哪里尝试在代码中添加断点。

这是我的MapActivity.java的onCreate方法中的代码:

 /*
 CODE FOR DISTANCE TEXT AND SHARE BUTTON
 */
    if(getSupportActionBar() != null) {
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }
    Toolbar toolbar = findViewById(R.id.toolbar);
    mDrawerLayout = findViewById(R.id.nav_drawer);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.open, R.string.close);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();

    mNavigationView = findViewById(R.id.navigation_view);
    if(mNavigationView != null) {
        mNavigationView.setNavigationItemSelectedListener(this);
    }

以下是我的AndroidManifest.xml中的活动

<application
        android:allowBackup="true"
        android:icon="@drawable/wheres_my_ride_icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="***************************" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".MapActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
        </activity>
        <activity
            android:name=".StartActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".TutorialActivity"
            android:label="@string/title_activity_tutorial"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>
    </application>

这是我的styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Customize theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorSecondary">@android:color/white</item>
    </style>

    <!-- Action Bar Custom Style -->
    <style name="MyActionBar" parent="Theme.AppCompat.Light">
        <item name="android:background">#2a363b</item>
        <item name="drawerArrowStyle">@style/DrawerHamburgerStyle</item>
        <item name="android:textColorSecondary">@android:color/white</item>
    </style>

    <!-- Action Bar Icon Style -->
    <style name="DrawerHamburgerStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/white</item>
    </style>

    <!--  Custom Alert Dialog Theme -->
    <style name="CustomAlertDialog" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

这是我的toolbar.xml

 <?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"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.design.widget.AppBarLayout
        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"
            android:gravity="center"
            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>

</LinearLayout>

这里是我在activity_map.xml中包含toolbar.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nav_drawer">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- RIGHT HERE IS THE TOOLBAR -->
    <include layout="@layout/toolbar"/>

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.christopher_sheridan.wheres_my_ride.MapActivity"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />

1 个答案:

答案 0 :(得分:1)

在阅读了布局后,我意识到我忘记了z排序!

我改变了我的代码:

    <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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nav_drawer">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- THIS WAS MY OOPSIE -->

    <include layout="@layout/toolbar"/>

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.christopher_sheridan.wheres_my_ride.MapActivity"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />

到此:

<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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nav_drawer">


<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.christopher_sheridan.wheres_my_ride.MapActivity"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />

    <!-- PLACED IT UNDER THE FRAGMENT -->
    <include layout="@layout/toolbar"/>

现在工具栏就在那里。 : - )