当前位置:   article > 正文

安卓Design之NavigationView的使用

design_navigation

前面讲解了Design包下的TabLayout的使用,下面将带来NavagationView和DrawLayout以及toolbar的联动。

项目已经同步至:https://github.com/nanchen2251/designNavigation-and-tabLayout

NavigationView 通过提供抽屉导航所需的框架让实现更简单,同时它还能够直接通过菜单资源文件直接生成导航元素。把NavigationView作为DrawerLayout的内容视图来使用。NavigationView处理好了和状态栏的关系,可以确保NavigationView在API21+设备上正确的和状态栏交互。

上运行图:

 

以下代码在前面代码的基础上修改。

MainActivity.java

  1. package com.example.nanchen.designtablayoutdemo;
  2. import android.os.Bundle;
  3. import android.support.design.widget.NavigationView;
  4. import android.support.design.widget.TabLayout;
  5. import android.support.v4.view.GravityCompat;
  6. import android.support.v4.view.ViewPager;
  7. import android.support.v4.widget.DrawerLayout;
  8. import android.support.v7.app.ActionBarDrawerToggle;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.support.v7.widget.Toolbar;
  11. import android.view.MenuItem;
  12. import android.widget.Toast;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
  16. private DrawerLayout drawerLayout;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. ViewPager vp = (ViewPager) findViewById(R.id.main_vp);
  22. TabLayout tabLayout = (TabLayout) findViewById(R.id.main_tab_layout);
  23. Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
  24. drawerLayout = (DrawerLayout) findViewById(R.id.main_drawerlayout);
  25. //设置Toolbar和DrawerLayout实现动画和联动
  26. this.setSupportActionBar(toolbar);
  27. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,0,0);
  28. drawerLayout.addDrawerListener(toggle);//设置监听
  29. toggle.syncState();//加上同步
  30. getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
  31. List<String> list = new ArrayList<>();
  32. for (int i = 1; i < 5; i++) {
  33. // list.add(String.format(Locale.CHINA,"第02d%页",i));
  34. list.add("第"+i+"页");
  35. }
  36. vp.setAdapter(new MyAdapter(getSupportFragmentManager(),list));
  37. tabLayout.setupWithViewPager(vp);
  38. NavigationView navigationView = (NavigationView) findViewById(R.id.main_navigation);
  39. navigationView.setNavigationItemSelectedListener(this);
  40. }
  41. @Override
  42. public boolean onNavigationItemSelected(MenuItem item) {
  43. switch (item.getItemId()) {
  44. case R.id.menu_item1:
  45. Toast.makeText(this,"你点击了菜单1",Toast.LENGTH_SHORT).show();
  46. break;
  47. case R.id.menu_item2:
  48. Toast.makeText(this,"你点击了菜单2",Toast.LENGTH_SHORT).show();
  49. break;
  50. case R.id.menu_item3:
  51. Toast.makeText(this,"你点击了菜单3",Toast.LENGTH_SHORT).show();
  52. break;
  53. case R.id.menu_item4:
  54. Toast.makeText(this,"你点击了菜单4",Toast.LENGTH_SHORT).show();
  55. break;
  56. }
  57. drawerLayout.closeDrawer(GravityCompat.START);
  58. return true;
  59. }
  60. }

主布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. tools:context="com.example.nanchen.designtablayoutdemo.MainActivity">
  10. <!--Toolbar,ActionBar的替代品-->
  11. <android.support.v7.widget.Toolbar
  12. android:id="@+id/main_toolbar"
  13. app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  14. android:background="@color/colorPrimary"
  15. android:layout_width="match_parent"
  16. android:layout_height="?actionBarSize"/>
  17. <android.support.v4.widget.DrawerLayout
  18. android:layout_width="match_parent"
  19. android:layout_height="0dp"
  20. android:id="@+id/main_drawerlayout"
  21. android:layout_weight="1">
  22. <LinearLayout
  23. android:orientation="vertical"
  24. android:layout_width="match_parent"
  25. android:layout_height="match_parent">
  26. <android.support.design.widget.TabLayout
  27. android:layout_width="match_parent"
  28. app:tabBackground="@color/colorPrimaryDark"
  29. app:tabTextColor="@android:color/white"
  30. app:tabSelectedTextColor="#04b4ae"
  31. android:layout_height="wrap_content"
  32. android:id="@+id/main_tab_layout"/>
  33. <android.support.v4.view.ViewPager
  34. android:layout_width="match_parent"
  35. android:layout_height="0dp"
  36. android:layout_weight="1"
  37. android:id="@+id/main_vp"/>
  38. </LinearLayout>
  39. <android.support.design.widget.NavigationView
  40. app:menu="@menu/navigation"
  41. android:id="@+id/main_navigation"
  42. android:layout_gravity="start"
  43. app:headerLayout="@layout/header"
  44. app:itemIconTint="@color/navigation_selector"
  45. app:itemTextColor="@color/navigation_selector"
  46. android:layout_width="match_parent"
  47. android:layout_height="match_parent">
  48. </android.support.design.widget.NavigationView>
  49. </android.support.v4.widget.DrawerLayout>
  50. </LinearLayout>

  

 

 

 navigation.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  3. <group android:checkableBehavior="single">
  4. <item android:id="@+id/single_1" android:title="单选1" android:icon="@mipmap/ic_launcher"/>
  5. <item android:id="@+id/single_2" android:title="单选2" android:icon="@mipmap/ic_launcher"/>
  6. <item android:id="@+id/single_3" android:title="单选3" android:icon="@mipmap/ic_launcher"/>
  7. </group>
  8. <item android:id="@+id/menu_item1" android:title="菜单1" android:icon="@mipmap/ic_launcher"/>
  9. <item android:id="@+id/menu_item2" android:title="菜单2" android:icon="@mipmap/ic_launcher"/>
  10. <item android:id="@+id/menu_item3" android:title="菜单3" android:icon="@mipmap/ic_launcher"/>
  11. <item android:id="@+id/menu_item4" android:title="菜单4" android:icon="@mipmap/ic_launcher"/>
  12. </menu>

  

header.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:orientation="vertical"
  5. android:gravity="center"
  6. android:background="@drawable/header_bg"
  7. android:layout_height="200dp">
  8. <ImageView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:src="@mipmap/ic_launcher"/>
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="文本"/>
  16. </LinearLayout>

  

header.bg.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  3. <gradient android:type="linear" android:angle="45"
  4. android:startColor="#143e52" android:endColor="#06a0ff"/>
  5. </shape>

  

navigation_selector.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:color="#fa08ca" android:state_checked="true"/>
  4. <item android:color="#6b6a6a" android:state_checked="false"/>
  5. </selector>

  

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/219229
推荐阅读
相关标签
  

闽ICP备14008679号