我的问题是让我的工具栏和导航抽屉显示在新的布局文件上。除了新的标签式布局视图外,它适用于整个应用程序。
我尝试在新的java类的onCreate中激活它:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_days);
mToolBar = activateToolbar();
setUpNavigationDrawer();
我不再收到错误,但工具栏仍然无法显示。我想知道我的布局文件是否有问题。
baseactivity:
public static final int HOME = 1;
public static final int MY_WORKOUT = 2;
public static final int MAXES = 3;
public static final int PROGRESS = 4;
public static final int WORKOUTS = 5;
public static final int HELP = 6;
private Class mNextActivity;
public static String mTitle = AppConstant.SHIEKO_WORKOUT;
public static int mType = HOME;
protected Toolbar mToolBar;
private NavigationDrawerFragment mDrawerFragment;
protected Toolbar activateToolbar() {
if(mToolBar == null) {
mToolBar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(mToolBar);
switch (mType) {
case HOME:
getSupportActionBar().setTitle(AppConstant.SHIEKO_WORKOUT);
break;
case MY_WORKOUT:
getSupportActionBar().setTitle(AppConstant.MY_WORKOUT);
break;
case MAXES:
getSupportActionBar().setTitle(AppConstant.SET_MY_MAXES);
break;
case PROGRESS:
getSupportActionBar().setTitle(AppConstant.MY_PROGRESS);
break;
case WORKOUTS:
getSupportActionBar().setTitle(AppConstant.WORKOUTS);
break;
case HELP:
getSupportActionBar().setTitle(AppConstant.FEEDBACK);
}
}
return mToolBar;
}
这是我的活动:我还有3个其他片段xml页面,分别用于3个选项卡,那些不包括工具栏或导航栏,因为我的理解他们在tabview下面..纠正我,如果我错了。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--<RelativeLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent">-->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="0dp">
<include android:id="@+id/app_bar"
layout="@layout/toolbar" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<!--</RelativeLayout>-->
<fragment
android:name="com.bestworkouts.sheikoworkout.NavigationDrawerFragment"
android:id="@+id/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
提前致谢!
答案 0 :(得分:0)
你的布局应该是这样的:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout>
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout>
<android.support.v7.widget.Toolbar/>
</android.support.design.widget.AppBarLayout>
<ViewPager>
</android.support.design.widget.CoordinatorLayout>
<!-- Slider Menu -->
<android.support.design.widget.NavigationView/>
</android.support.v4.widget.DrawerLayout>
顺便说一下,无需致电getSupportActionBar().setTitle(AppConstant.WORKOUTS)
,您只需致电setTitle()
即可设置actionBar的标题。