我正在尝试将DrawerLayout与ActionBarDrawerToggle一起使用。 看下面的代码:
public class MainActivity extends ActionBarActivity {
private static final String TAG = MainActivity.class.getName();
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
FeedsFragment feeds = new FeedsFragment();
if (findViewById(R.id.main) != null) {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportFragmentManager().beginTransaction().add(R.id.main, feeds).commit();
} else if (findViewById(R.id.content) != null) {
NavDrawerFragment nav = new NavDrawerFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.nav, nav)
.add(R.id.content, feeds)
.commit();
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
mDrawerToggle.onConfigurationChanged(config);
}
}
和我的观点:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--<include layout="@layout/nav" />-->
<fragment
android:layout_width="240dp"
android:layout_height="match_parent"
android:name="com.primeirochute.com.primeirochute.fragment.NavDrawerFragment"
tools:layout="@layout/nav" />
</android.support.v4.widget.DrawerLayout>
但是当我运行我的项目时,我的屏幕上显示的左侧菜单(DrawerLayout)不会产生滑块事件。
答案 0 :(得分:2)
最终作品:
我刚刚添加了引力=&#34;开始&#34;在我的第二个观点
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--<include layout="@layout/nav" />-->
<fragment
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.primeirochute.com.primeirochute.fragment.NavDrawerFragment"
tools:layout="@layout/nav" />
</android.support.v4.widget.DrawerLayout>
答案 1 :(得分:0)
使用功能onOptionsItemSelected并控制抽屉事件:
public boolean onOptionsItemSelected(MenuItem item) {
int itemID = item.getItemId();
switch (itemID) {
case android.R.id.home: {
DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
if (drawerLayout.isDrawerOpen(Gravity.LEFT)) {
drawerLayout.closeDrawers();
}
else {
drawerLayout.openDrawer(Gravity.LEFT);
}
}
break;
return true;
}
我希望它适用于您的情况。