MainActivity :
package com.example.month_three; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Gravity; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Toolbar toolbar; private DrawerLayout drawerLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout); toolbar.inflateMenu(R.menu.toolbar); //添加选项菜单到Toolbar上 toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { drawerLayout.openDrawer(Gravity.LEFT); //弹出左边的抽屉 } }); // 给Toolbar上的选项菜单添加点击事件 toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.search: Toast.makeText(MainActivity.this,"您点击了搜索按钮!!",Toast.LENGTH_LONG).show(); break; } return false; } }); // 1。获取碎片管理者 android.app.FragmentManager manager = getFragmentManager(); // 2.获取事物 android.app.FragmentTransaction transaction = manager.beginTransaction(); // 3.替换布局为需要的Fragment对象 transaction.replace(R.id.layout,new Call_LogFragment()); transaction.commit(); //提交事物 } // 把Fragment加载到Activity当中的方法,动态加载法 public void onClick(View view) { switch (view.getId()) { case R.id.btn_call: // 1。获取碎片管理者 android.app.FragmentManager manager = getFragmentManager(); // 2.获取事物 android.app.FragmentTransaction transaction = manager.beginTransaction(); // 3. transaction.replace(R.id.layout,new Call_LogFragment()); transaction.commit(); // 关闭抽屉 drawerLayout.closeDrawers(); break; case R.id.btn_sms: android.app.FragmentManager manager1 = getFragmentManager(); android.app.FragmentTransaction transaction1 = manager1.beginTransaction(); transaction1.replace(R.id.layout,new SmsFragment()); transaction1.commit(); // 关闭抽屉 drawerLayout.closeDrawers(); break; case R.id.btn_music: android.app.FragmentManager manager2 = getFragmentManager(); android.app.FragmentTransaction transaction2 = manager2.beginTransaction(); transaction2.replace(R.id.layout,new MusicFragment()); transaction2.commit(); // 关闭抽屉 drawerLayout.closeDrawers(); break; case R.id.btn_net: android.app.FragmentManager manager3 = getFragmentManager(); android.app.FragmentTransaction transaction3 = manager3.beginTransaction(); transaction3.replace(R.id.layout,new NetFragment()); transaction3.commit(); drawerLayout.closeDrawers(); break; } } }
mainLayout :
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/drawerlayout"> <!-- 代表中心的内容--> <LinearLayout android:id="@+id/center_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:title="通话记录" app:titleTextColor="#FFF" android:background="@color/colorPrimary" app:navigationIcon="@mipmap/icon_nav"> </android.support.v7.widget.Toolbar> <!-- 用来占位的--> <LinearLayout android:id="@+id/layout" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout> </LinearLayout> <!-- 代表左边的抽屉 --> <LinearLayout android:id="@+id/left_layout" android:layout_width="200dp" android:layout_height="match_parent" android:orientation="vertical" android:layout_gravity="left" android:background="#007700"> <RelativeLayout android:layout_width="match_parent" android:layout_height="200dp" android:background="@color/colorAccent"> <ImageView android:id="@+id/iv" android:layout_centerInParent="true" android:src="@mipmap/ic_launcher" android:layout_width="80dp" android:layout_height="80dp" /> <TextView android:layout_below="@+id/iv" android:gravity="center" android:text="考试必过" android:textSize="18sp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> <Button android:id="@+id/btn_call" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取通话记录" android:onClick="onClick"/> <Button android:id="@+id/btn_sms" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取短信记录" android:onClick="onClick"/> <Button android:id="@+id/btn_music" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取多媒体音乐" android:onClick="onClick"/> <Button android:id="@+id/btn_net" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取网络信息" android:onClick="onClick"/> </LinearLayout> </android.support.v4.widget.DrawerLayout>