答案 0 :(得分:1)
在创建drawable
时定义ActionBarDrawerToggle
后,请确保将以下内容添加到Activity
以便进行滑动效果:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
答案 1 :(得分:0)
使用https://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html
您将drawable提供给ActionBarToggle实例,它会为您处理它。
new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close);
它需要一个用于打开和关闭的字符串,一个图标的图像以及附加到它的DrawerLayout。
这是我在我的一个应用程序中扩展我所有活动的课程。它确保抽屉在我的所有活动中都能正常工作。
import android.app.ActionBar;
import android.app.ActionBar.LayoutParams;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class ActionBarActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
String title;
View customNav;
SessionManager sManager;
public String getActionBarTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
TextView titleView = (TextView) customNav.findViewById(R.id.title);
titleView.setText(title);
}
public void closeDrawer(int gravity) {
mDrawerLayout.closeDrawer(gravity);
}
public void openDrawer(int gravity) {
mDrawerLayout.openDrawer(gravity);
}
public void onCreate(Bundle savedInstanceState, DrawerLayout layout,
String title) {
super.onCreate(savedInstanceState);
this.title = title;
sManager = ((UberApplication) getApplication()).getSessionManager();
mDrawerLayout = layout;
ActionBar actionBar = getActionBar();
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
customNav = LayoutInflater.from(this)
.inflate(R.layout.action_bar, null);
actionBar.setDisplayHomeAsUpEnabled(mDrawerLayout != null);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setHomeButtonEnabled(true);
actionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#5385cc")));
actionBar.setCustomView(customNav, lp);
actionBar.setDisplayShowCustomEnabled(true);
if (sManager.getUnreadNotificationCount() > 0) {
Badge badge = (Badge) customNav.findViewById(R.id.badge_holder);
badge.setBadgeCount(Integer.toString(sManager
.getUnreadNotificationCount()));
badge.setVisibility(View.VISIBLE);
badge.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
closeDrawer(Gravity.START);
}
});
}
TextView titleView = (TextView) customNav.findViewById(R.id.title);
titleView.setText(title);
if (mDrawerLayout != null) {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (((UberApplication) getApplication()).isFirstLaunchForVersion()) {
mDrawerLayout.postDelayed(new Runnable() {
@Override
public void run() {
mDrawerLayout.openDrawer(Gravity.START);
}
}, 600);
}
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (mDrawerLayout != null) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(Gravity.START);
}
return super.onPrepareOptionsMenu(menu);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
if (mDrawerLayout != null) {
mDrawerToggle.syncState();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (mDrawerLayout != null) {
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerLayout != null) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
}
return super.onOptionsItemSelected(item);
}
}