DrawerLayout能够让我们在项目中非常方便地实现側滑菜单效果。如今主流的应用如QQ等都

採用的这样的效果。

这两天也是在学习Android Design Support的相关知识。网上有关这方面的文章介绍非常多。可是为了方便以后使用,还是把学习的知识做个简单记录。这次的代码也是在上一篇博客Android Design Support控件介绍之TabLayout的基础上加入的布局和代码。

主界面布局:

<?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"
android:id="@+id/drawer_view"
android:layout_width="match_parent"
android:layout_height="match_parent"> <android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:fitsSystemWindows="true"
app:headerLayout="@layout/content_main"><!--content_main与上篇文章中一样-->
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

側滑菜单左側的布局

<?xml version="1.0" encoding="utf-8"?

>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="菜单ONE" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="菜单TWO" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="菜单THREE" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="菜单FOUR" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="菜单FIVE" />
</LinearLayout>

Fragment界面布局

<?

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/content_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="菜单ONE" />
</LinearLayout>

主界面代码,TestAdapter 及TestFragment代码也同上一篇文章一样。

public class MainActivity extends FragmentActivity {
//便捷实现标签显示
private TabLayout tab_layout;
private ViewPager viewpager;
private TestAdapter mAdapter;
private List<Fragment> fragments;
private List<String> titles;
private DrawerLayout drawer_view; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
initViews();
initViewPages();
} private void initViewPages() {
fragments = new ArrayList<>();
titles = new ArrayList<>();
for (int i = 0; i < 4; i++) {
String title="TAB" + i;
titles.add(title);
tab_layout.addTab(tab_layout.newTab().setText(title));//加入Tab标题
Fragment fragment = new TestFragment();
Bundle bundle = new Bundle();
bundle.putString("content", "这是第" + i + "个Fragment页面");
fragment.setArguments(bundle);
fragments.add(fragment);
} mAdapter = new TestAdapter(this.getSupportFragmentManager(), titles, fragments);
viewpager.setAdapter(mAdapter);
tab_layout.setupWithViewPager(viewpager);
tab_layout.setTabsFromPagerAdapter(mAdapter); } private void initViews() {
tab_layout = (TabLayout) findViewById(R.id.tab_layout);
viewpager = (ViewPager) findViewById(R.id.viewpager);
drawer_view=(DrawerLayout)findViewById(R.id.drawer_view);
drawer_view.closeDrawer(GravityCompat.START);
}
}

效果图:

Android Design Support控件之DrawerLayout简单使用的更多相关文章

  1. android design 新控件

    转载请标明出处: http://blog.csdn.net/forezp/article/details/51873137 本文出自方志朋的博客 最近在研究android 开发的新控件,包括drawe ...

  2. Android Design Support Library介绍之:环境搭建

    在2015年的GoogleIO大会上.具体的Material Design设计规范出炉了.全新的Android Design Support Library 格.更让人开心的是,这些很酷的风格能够通过 ...

  3. Android Design Support Library 中控件的使用简单介绍(一)

    Android Design Support Library 中控件的使用简单介绍(一) 介绍 在这个 Lib 中主要包含了 8 个新的 material design 组件!最低支持 Android ...

  4. MaterialEditText——Android Material Design EditText控件

    MaterialEditText是Android Material Design EditText控件.可以定制浮动标签.主要颜色.默认的错误颜色等. 随着 Material Design 的到来, ...

  5. Android基本控件Spinner的简单使用【转】

    Android基本控件Spinner的简单使用 感谢大佬:https://blog.csdn.net/bingocoder/article/details/80469939 学习过了Textview, ...

  6. Android Design Support Library使用详解

    Android Design Support Library使用详解 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的And ...

  7. 一个Activity掌握Design新控件 (转)

    原文地址:http://blog.csdn.net/lavor_zl/article/details/51295364 谷歌在推出Android5.0的同时推出了全新的设计Material Desig ...

  8. 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...

  9. 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用

    Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...

随机推荐

  1. nj03---阻塞和线程

    Node.js最大的特性就是"异步式I/O"与事件紧密结合的编程模式.这种模式与传统的同步式IO线性的编程思路有很大的不同,因为控制流很大程度上要靠"事件"和& ...

  2. 曲根英语万词---二、evoke

    曲根英语万词---二.evoke 一.总结 一句话总结:evoke v.唤起,引起 词根:-voc-, -vok- [词根含义]:声音,叫喊 1.consecrate? v,供奉,奉为神圣 -ate, ...

  3. C#篇(三)——函数传参之引用类型和值类型

    首先应该认清楚在C#中只有两种类型: 1.引用类型(任何称为"类"的类型) 2.值类型(结构或枚举) 先来认识一下引用类型和值类型的区别: 函数传参之引用类型: 1.先来一个简单的 ...

  4. CUDA笔记(七)

    今天集中时间找程序的问题.于是发现: 首先,程序里的kernel想要调试,必须用nsight. 于是一堆找.http://www.nvidia.com/object/nsight.html http: ...

  5. Android 自定义viewpager 三张图片在同一屏幕轮播的效果

    github:https://github.com/nickeyCode/RoundImageViewPager 说实话不知道怎么描述这个效果,在网页上见得跟多,公司要求做这个效果得时候不知道怎么用文 ...

  6. Android RecyclerView实现横向滚动

    我相信很久以前,大家在谈横向图片轮播是时候,优先会选择具有HorizontalScrollView效果和ViewPager来做,不过自从Google大会之后,系统为我们提供了另一个控件Recycler ...

  7. iview中 ...用法

    1. 2. 3. 4.可以将divs转为数组解构 5. 解构 6.作为函数的参数 7.作为参数遍历

  8. Ubuntu18.04 更改GRUB引导菜单背景图片和默认启动项

    一.更改GRUB引导菜单背景图片1.首先准备一张想要的照片,文件名是啥无所谓,只要格式是*.jpg *.JPG *.jpeg *.JPEG *.png *.PNG *.tga *.TGA都行,都能自动 ...

  9. Bomb HDU - 3555 数位dp

    Code: #include<cstdio> #include<algorithm> #include<cstring> #include<string> ...

  10. 软raid 实验

    RAID0 条带卷 2+ 100% 读写速度快,不容错 RAID1 镜像卷 2 50% 读写速度一般,容错 RAID5 带奇偶校验的条带卷 3+ (n-1)/n 读写速度快,容错,允许坏一块盘 RAI ...