【问题标题】:Android navigation drawer toolbar is missing缺少 Android 导航抽屉工具栏
【发布时间】:2016-04-28 17:29:08
【问题描述】:

我有一个带有导航抽屉的启动器活动。当我将地图添加到该启动器活动中时,我看不到任何工具栏,因此我无法控制导航抽屉。这是我的活动现在的样子:

这里应该是什么样子(我需要这个工具栏)

并且导航抽屉应该像这样与地图重叠

这是我的代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map_nav);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    mapView = new MapView(this);

    MapHandler.getMapHandler().init(this, mapView);

    addMarkers();
}

如您所见,我将工具栏添加到活动中

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

这是 MapHandler init method() 的一部分

mapView.getLayerManager().getLayers().add(tileRendererLayer);
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    activity.addContentView(mapView, params);

这是清单文件中的代码

    <activity
        android:name=".map.MapNavActivity"
        android:theme="@style/AppTheme.NoActionBar"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

那么我做错了什么?

编辑我的导航活动 xml

<?xml version="1.0" encoding="utf-8"?>
<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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_map_nav"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_map_nav"
        app:menu="@menu/activity_map_nav_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar_map_nav.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="aplikacija.zveju.rinkodara.com.zvejuaplikacija.map.MapNavActivity">

    <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="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 请添加您的活动 xml 布局。
  • @Majestic 我添加 xml 文件
  • 这只是猜测,但也许您将 CoordinatorLayout 与地图一起使用?如果是这样,AppBar 将仅显示附加的行为
  • @David 我看不到您将 mapView 附加到布局的位置。我认为,它与您的收费栏重叠。尝试在 content_map_nav.xml 中添加 MapView 并在 onCreate() 中尝试 findViewById()。
  • 可能是因为风格检查你在资源中的风格

标签: android navigation-drawer android-toolbar


【解决方案1】:

我在 Majestic 的帮助下解决了这个问题。谢谢!我将此代码添加到content_map_nav.xml

<org.mapsforge.map.android.view.MapView
    android:id="@+id/mapView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">
</org.mapsforge.map.android.view.MapView>

在 mainactivity 中而不是这个 mapView = new MapView() 我写这个 mapView = (MapView) findViewById(R.id.mapView); 现在它可以工作了!

【讨论】:

    【解决方案2】:

    可能你已经用RelativeLayout或LinearLayout替换了content_main.xml中的ConstraintLayout,如果你想使用LinearLayout或RelativeLayout你应该保留但像这样包装你的布局:

      <?xml version="1.0" encoding="utf-8"?>
     <android.support.constraint.ConstraintLayout 
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.gmail.farhadkargaran.myapplication.MainActivity"
    tools:showIn="@layout/app_bar_main">
    
    <LinearLayout
    
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/main_bg"
        android:orientation="vertical"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:showIn="@layout/app_bar_main">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2015-02-26
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2016-03-26
      • 2019-09-15
      相关资源
      最近更新 更多