Android导航抽屉没有添加ActionBar菜单按钮

时间:2015-06-02 03:40:56

标签: android android-fragments navigation-drawer slidingdrawer drawerlayout

我的程序目前允许我通过滑动手指打开导航栏但不会显示菜单按钮,因此我可以单击菜单按钮将其打开。我有onPostCreate和onOptionsItem Overide函数,但我不相信它们会被调用。我该如何解决这个问题。 我的程序最低API级别为8,所以我不知道这是不是问题。谢谢!

主要活动:

public class Home_Page extends ActionBarActivity implements AdapterView.OnItemClickListener{

NavigationDrawer drawerLayout;
ListView listViewLeft, listViewRight;
String selectedMenuItem;
MyListViewAdapter myListViewAdapter;
int[] images = {R.drawable.menu_icon, R.drawable.menu_icon, R.drawable.menu_icon, R.drawable.menu_icon};
String[] listViewLeftItems = {"Home", "Choice 2", "Choice 3", "Choice 4"};
Intent intent;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home__page);

    drawerLayout = new NavigationDrawer(this, (DrawerLayout) findViewById(R.id.drawerLayout), getSupportActionBar());
    drawerLayout.createDrawer();

    initializeVar();

    myListViewAdapter = new MyListViewAdapter(this, images, listViewLeftItems);
    listViewLeft.setAdapter(myListViewAdapter);
    listViewLeft.setOnItemClickListener(this);

}

public void initializeVar(){
    listViewLeft = (ListView) findViewById(R.id.drawerListLeft);
    listViewRight = (ListView) findViewById(R.id.drawerListRight);
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    drawerLayout.closeDrawers();
    switch(position){
        case 0:
            selectListViewItemLeft(position);
            new Thread() {
                public void run() {
                    try {
                        intent = new Intent(Home_Page.this, Home_Page.class);
                        startActivity(intent);
                        finish();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }.start();
            break;
        case 1:
            selectListViewItemLeft(position);
            new Thread() {
                public void run() {
                    try {
                        Intent intent = new Intent(Home_Page.this, AddAthlete.class);
                        startActivity(intent);
                        finish();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }.start();
            break;
        case 2:
            selectListViewItemLeft(position);
            break;
        case 3:
            selectListViewItemLeft(position);
            break;
    }
}

public void selectListViewItemLeft(int position){
    listViewLeft.setItemChecked(position, true);
    selectedMenuItem = listViewLeftItems[position];
}
}

导航抽屉类:(自定义类可以在不同的活动中创建新的导航栏)

public class NavigationDrawer extends Activity{
DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
Activity currentActivity;
android.support.v7.app.ActionBar actionBar;

NavigationDrawer(Context context, DrawerLayout drawerLayout, android.support.v7.app.ActionBar actionBar) {
    this.currentActivity = (Activity) context;
    this.drawerLayout = drawerLayout;
    this.actionBar = actionBar;
}

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void createDrawer() {

    drawerToggle = new ActionBarDrawerToggle(currentActivity, drawerLayout, R.drawable.menu_icon, R.string.drawer_open, R.string.drawer_closed) {
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            actionBar.setTitle("Menu");
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            actionBar.setTitle(getTitle());
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setIcon(android.R.color.transparent);
    actionBar.setHomeButtonEnabled(true);
}
public void closeDrawers(){
    drawerLayout.closeDrawers();
}

// Displays toggle button to expand drawer
@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
    Log.e("", "onPostCreate Run");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(drawerToggle.onOptionsItemSelected(item)){
        Log.e("", "onOptionsItemSelected Run");
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
    Log.e("", "onConfigurationChanged Run");
}

}

1 个答案:

答案 0 :(得分:3)

在活动的布局文件中,使用抽屉布局作为父布局,并将工具栏添加为子视图。例如:

    <?xml version="1.0" encoding="utf-8"?>
        <android.support.v4.widget.DrawerLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/DrawerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:elevation="7dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical>

                <include
                    android:id="@+id/tool_bar"
                    layout="@layout/tool_bar"></include>
                <!-- Add your Main Content Here -->

            </LinearLayout>

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="left">

            <!-- Add your Drawer layout content here -->

             </FrameLayout>


        </android.support.v4.widget.DrawerLayout>

您的工具栏布局tool_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar                                                  `       xmlns:android="http://schemas.android.com/apk/res/android"`
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/action_bar_color"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

</android.support.v7.widget.Toolbar>