带工具栏的导航抽屉在多个活动中的实现最新代码

Navigation Drawer with toolbar Implementation in multiple activites latest code

我在 base activity 中创建了导航抽屉并在 main activity 中扩展,但在 main activity

中没有结果

这是我的代码:

BaseActivity Java代码:

public class BaseActivity extends AppCompatActivity {
        DrawerLayout mDrawerLayout;
        ListView mDrawerList;
        String[] items;
        ActionBarDrawerToggle mDrawerToggle;
        Toolbar toolbar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerList = (ListView) findViewById(R.id.drawer_list);
            toolbar = (Toolbar) findViewById(R.id.tool_bar);
            setSupportActionBar(toolbar);
            items = getResources().getStringArray(R.array.menu);
            mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, items));
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close){

            };
            mDrawerLayout.addDrawerListener(mDrawerToggle);
            mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    selectItem(position);
                    mDrawerLayout.closeDrawer(mDrawerList);

                }
            });

        }
        public void selectItem(int position){
            Intent intent;
            switch (position)
            {
                case 0:
                    intent = new Intent(this,OneActivity.class);
                    startActivity(intent);
                    break;
                case 1:
                    intent = new Intent(this,TwoActivity.class);
                    startActivity(intent);
                    break;
            }
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);

        }
        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            mDrawerToggle.syncState();
        }
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            mDrawerToggle.onConfigurationChanged(newConfig);
        }

    }

这是我的 Xml 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:toolbar="http://schemas.android.com/apk/res-auto"
            android:id="@+id/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            toolbar:navigationIcon="@drawable/ic_navigation">

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

        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <ListView
                android:id="@+id/drawer_list"
                android:layout_width="240dp"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:background="@color/colorAccent"
                android:choiceMode="singleChoice"
                android:divider="@android:color/holo_blue_dark"
                android:dividerHeight="1dp">

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

这是 MainActivity:

 public class MainActivity extends BaseActivity {

         protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(layout.activity_main);

                    }

        }

给我建议正确答案

baseActivity class 中创建构造函数并在构造函数中调用 baseActivity class 的定义函数,然后在 MainActivity class 函数 onCreate(Bundle savedInstanceState).