我已阅读NavigationDrawer的文档,并尝试在自己的应用程序中创建它。但是有一个问题:应该用作菜单的ListView只浮动在主要内容上,我无法用它执行任何操作(例如,通过滑动关闭)。有什么问题?
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/mainPageLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<android.support.v4.view.ViewPager
android:id="@+id/mainScreenViewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<android.support.v4.view.PagerTitleStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
</FrameLayout>
</LinearLayout>
<ListView
android:id="@+id/navigationDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:gravity="start" >
</ListView>
</android.support.v4.widget.DrawerLayout>
抽屉初始化:
String[] titles = getResources().getStringArray(R.array.lists_titles);
ListView drawer = (ListView) findViewById(R.id.navigationDrawer);
drawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles));
drawer.setOnItemClickListener(new DrawerItemClickListener());
感谢。
答案 0 :(得分:2)
以下是适用于我的代码的粗略示例,它们是android developers site和StylingAndroid blog的组合:
DrawerLayout mDrawerLayout;
ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layoutxml_id);
// must initialize mDrawerLayout and mDrawerToggle in main thread
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
// do something
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
// do something
}
};
String[] titles = getResources().getStringArray(R.array.lists_titles);
ListView drawer = (ListView) findViewById(R.id.navigationDrawer);
drawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles));
drawer.setOnItemClickListener(new DrawerItemClickListener());
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeButtonEnabled(true);
}
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
if(item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
mDrawerLayout.openDrawer(GravityCompat.START);
}
}
return super.onOptionsItemSelected(item);
}