Android Design: 九种常见Activity及代码解析之"Navigation Drawer Activity"
来源:互联网 发布:java 自定义构造函数 编辑:程序博客网 时间:2024/05/17 01:41
Navigation Drawer Activity
如果我们是直接new一个“Navigation Drawer Activity”,自动生成的代码如下:
NavigationDrawerActivity.java
public class NavigationDrawerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_navigation_drawer); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.navigation_drawer, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here. int id = item.getItemId(); if (id == R.id.nav_camara) { // Handle the camera action } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; }}
activity_navigation_drawer
<?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:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_navigation_drawer" android:layout_width="match_parent" android:layout_height="match_parent"/> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_navigation_drawer" app:menu="@menu/activity_navigation_drawer_drawer"/></android.support.v4.widget.DrawerLayout>
app_bar_navigation_drawer.xml
<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true" tools:context="com.zhuanghongji.activitysamples.activitys.NavigationDrawerActivity"> <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"/> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_navigation_drawer"/> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email"/></android.support.design.widget.CoordinatorLayout>
0 0
- Android Design: 九种常见Activity及代码解析之"Navigation Drawer Activity"
- Android Design: 九种常见Activity及代码解析之"Login Activity"
- Android Material Design: 九种常见Activity及代码解析之"概要说明"
- Android Design: 九种常见Activity及代码解析之"Empty""Blank""Fullscreen"
- Navigation Drawer Activity布局
- Android Material Design系列之Navigation Drawer
- Navigation Drawer Activity设置头像
- Android之Navigation Drawer
- 学习Google新建项目 Navigation Drawer Activity
- Metarial design侧滑栏之Navigation Drawer
- Android Design 趋势——Navigation Drawer
- Android 开发之Navigation Drawer
- Navigation Drawer Activity在小米MIUI中的Toolbar按扭菜单的奇怪表现及修正方法
- Android Navigation Drawer
- Android学习 - Navigation Drawer
- Android Navigation Drawer
- fragment (4)用fragment自定义Tabbed Activity和Navigation drawer Activity
- android之 activity及activity栈详解
- 面试题22:栈的压入、弹出序列
- POJ1703 && POJ2942 &&POJ 1182 并查集 这个做法挺巧妙
- 数据库的优化
- Mysql数据库备份数据库与还原数据库
- HDU - 1200 To and Fro(水)
- Android Design: 九种常见Activity及代码解析之"Navigation Drawer Activity"
- SQL常用命令
- 查找二叉树
- EIP和目标地址的关系
- DirectX11 骷髅头示例Demo
- UVA 11177 凸多边形和圆交
- POJ - 2029 Get Many Persimmon Trees(暴力水题)
- Android控件布局属性全解
- swift语法讲解及简单入门教程