这是我的initDrawer代码...
public void initDrawerLayout(String toolbar_text) {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerRelative = (RelativeLayout) findViewById(R.id.drawer_relative);
if (mDrawerLayout != null) {
findViewById(R.id.drawer_btn_a).setOnClickListener(this);
findViewById(R.id.drawer_btn_b).setOnClickListener(this);
findViewById(R.id.drawer_btn_c).setOnClickListener(this);
findViewById(R.id.drawer_btn_d).setOnClickListener(this);
findViewById(R.id.drawer_btn_e).setOnClickListener(this);
findViewById(R.id.drawer_btn_f).setOnClickListener(this);
findViewById(R.id.drawer_btn_g).setOnClickListener(this);
findViewById(R.id.drawer_btn_h).setOnClickListener(this);
findViewById(R.id.drawer_btn_i).setOnClickListener(this);
findViewById(R.id.drawer_btn_j).setOnClickListener(this);
findViewById(R.id.drawer_btn_k).setOnClickListener(this);
findViewById(R.id.drawer_btn_l).setOnClickListener(this);
findViewById(R.id.my_layout).setOnClickListener(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setBackground(getResources().getDrawable(R.drawable.icn_actionbar_background));
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText(toolbar_text);
mTitle.setTypeface(Typeface.DEFAULT_BOLD);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
toolbar.setNavigationIcon(R.drawable.ic_drawer);
mDrawerToggle = new ActionBarDrawerToggle(
this, mDrawerLayout, toolbar,
R.string.drawer_open, R.string.drawer_close
);
mDrawerLayout.setDrawerListener(mDrawerToggle);
toolbar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
} else {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
}
});
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
mDrawerLayout.setScrimColor(getResources().getColor(
android.R.color.transparent));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
}
以下是我的片段代码:
((FirstActivity) getActivity()).initDrawerLayout(mFirst.name);
其中mFirst是Person类的一个对象。
注意工具栏代码:
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Toolbar Title"
android:textColor="@color/action_text-color"
android:textSize="18sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
请帮帮我,各位...