ToolBar、DrawerLayout和设置导航按钮

xiaoxiao2021-02-27  284

layout布局设计

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Light"/> </FrameLayout> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:text="This is menu" android:textSize="30sp" android:background="#FFF"/> </android.support.v4.widget.DrawerLayout> MainActivity.class

private DrawerLayout drawerLayout = null; Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); setSupportActionBar(toolbar); drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); ActionBar actionBar = getSupportActionBar(); //获取actionbar实例 if(actionBar != null){ actionBar.setDisplayHomeAsUpEnabled(true); //显示toolbar的home菜单 // actionBar.setHomeAsUpIndicator(R.drawable.menu); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.toolbar,menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.backup: Toast.makeText(this,"You Click Backup",Toast.LENGTH_LONG).show(); break; case R.id.delete: Toast.makeText(this,"You Click Delete",Toast.LENGTH_LONG).show(); break; case R.id.settings: Toast.makeText(this,"You Click Settings",Toast.LENGTH_LONG).show(); break; case android.R.id.home: drawerLayout.openDrawer(GravityCompat.START); break; default: break; } return true; } home的id是Android内规定的android.R.id.home。这是确定不变的

转载请注明原文地址: https://www.6miu.com/read-3760.html

最新回复(0)