简体   繁体   English

在不同活动中使用相同的导航抽屉,工具栏错误

[英]same navigation drawer in different activities, toolbar error

I'm trying to develop an app with multiple activities and the same navigation drawer and toolbar. 我正在尝试开发具有多个活动以及相同的导航抽屉和工具栏的应用程序。 I've created a BaseActivity for initialization of drawer and other activities extend it, but I have problems with toolbar, in particular when using setSupportActionBar(toolbar). 我已经创建了用于初始化抽屉的BaseActivity,并且其他活动对其进行了扩展,但是工具栏存在问题,尤其是在使用setSupportActionBar(toolbar)时。

How can I fix this problem? 我该如何解决这个问题?

BaseActivity.java BaseActivity.java

    public abstract class BaseActivity extends AppCompatActivity
  implements NavigationView.OnNavigationItemSelectedListener {

private DrawerLayout drawer;
private ActionBarDrawerToggle toggle;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutResourceId());

}

protected abstract int getLayoutResourceId();

public void initDrawer() {

   drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

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


   toggle = new ActionBarDrawerToggle(
            (Activity) this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

    drawer.setDrawerListener(toggle);
    toggle.setDrawerIndicatorEnabled(true);
    toggle.syncState();

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

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

}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.food) {
        final Intent intent = new Intent(this, FoodDiary.class);
        startActivity(intent);
    }

    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

activity_main.xml activity_main.xml中

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >

 <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_main" 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_main"
    app:menu="@menu/activity_main_drawer" />

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

app_bar_main.xml app_bar_main.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=".MainActivity">

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


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

FoodDiary.java FoodDiary.java

   public class FoodDiary extends BaseActivity {

        @Override
        protected int getLayoutResourceId() {
        return R.layout.content_fooddiary; 
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDrawer();
        ...
        }

content_fooddiary.xml content_fooddiary.xml

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:paddingBottom="@dimen/activity_vertical_margin"
       app:layout_behavior="@string/appbar_scrolling_view_behavior"

    <Space
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.5"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:src="@drawable/left2"/>

   </LinearLayout>

You can't use same Navigation Drawer and Toolbar with different Activities. 您不能将相同的导航抽屉和工具栏用于不同的活动。 If you want different screens with the same components, you have to use Fragments with one Activity, for example FragmentActivity. 如果希望具有相同组件的不同屏幕,则必须将Fragments与一个Activity一起使用,例如FragmentActivity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM