同一导航抽屉在不同的活动,工具栏错误

时间:2015-10-19 15:35:08

标签: android xml android-layout android-toolbar android-navigation-drawer

我正在尝试开发一个包含多个活动和相同导航抽屉和工具栏的应用。 我已经为抽屉的初始化创建了一个BaseActivity,其他活动扩展了它,但我的工具栏有问题,特别是在使用setSupportActionBar(工具栏)时。

如何解决此问题?

BaseActivity.java

<<eval=TRUE,echo=FALSE,results='asis',warning=FALSE,message=FALSE,error=FALSE>>=

test.xt <- xtable(test, label="table", caption='test')
align(test.xt) <- "|l|l|r|r|r|r|r|r|"
print(test.xt, tabular.environment='tabularx', include.rownames = FALSE,  width="\\textwidth", floating=FALSE)
@

activity_main.xml中

    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;
}

}

app_bar_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>

FoodDiary.java

  <?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>

content_fooddiary.xml

   public class FoodDiary extends BaseActivity {

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

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

1 个答案:

答案 0 :(得分:0)

您不能将相同的导航抽屉和工具栏与不同的活动一起使用。 如果您想要具有相同组件的不同屏幕,则必须将片段与一个活动一起使用,例如FragmentActivity。