Android Studio的侧滑Demo
来源:互联网 发布:中国f1车队知乎 编辑:程序博客网 时间:2024/05/20 04:27
Android Studio中带有一个非常流畅的侧滑Demo,也是谷歌官方支持开发者使用的一种规范,所以在最近使用到这个Demo时写上自己的一些用法。
1.首先 File-Project Structure -Modules-Dependencies中添加依赖
compile 'com.android.support:design:24.1.1'
写上相关依赖,最新即可
2.侧滑activity_main.xml 视图中的配置
<?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_main" 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_main" app:menu="@menu/activity_main_drawer" /></android.support.v4.widget.DrawerLayout>
侧滑还是依靠DrawerLayout实现的,区别在于谷歌官方用NavigationView集成了原本使用的ListView的功能。
NavigationView可以包含图片。且事件监听的方法更为方便,所以谷歌官方也推荐在或指定侧滑的一种标准。
fitSystemWindows属性:
官方描述: Boolean internal attribute to adjust view layout based on system windows such as the status bar. If true, adjusts the padding of this view to leave space for the system windows. Will only take effect if this view is in a non-embedded activity.简单描述: 这个一个boolean值的内部属性,让view可以根据系统窗口(如status bar)来调整自己的布局,如果值为true,就会调整view的paingding属性来给system windows留出空间....实际效果: 当status bar为透明或半透明时(4.4以上),系统会设置view的paddingTop值为一个适合的值(status bar的高度)让view的内容不被上拉到状态栏,当在不占据status bar的情况下(4.4以下)会设置paddingTop值为0(因为没有占据status bar所以不用留出空间)。
我理解是在DrawerLayout中使用半透明,在侧滑中也使用半透明。
关于<include
实际上是调用了一种布局。嵌套,便于代码的重构。或者冗余吧。
3.说明下layout下的app_bar_main.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.king.viewpager.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" 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_main" /></android.support.design.widget.CoordinatorLayout>
关于CoordinatorLayout
CoordinatorLayout作为“super-powered FrameLayout”基本实现两个功能: 1、作为顶层布局 2、调度协调子布局
关于Toolbr
ToolbarToobar主要是用来替换ActionBar的,换句话说,ActionBar能做的,Toolbar都能做。如果你对ActionBar的使用比较熟悉,你会发现Toolbar使用起来非常简单。ok,既然是替换,当然用Toolbar的时候就得先去把ActionBar给隐藏掉啦~
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:background="?attr/colorPrimary" android:layout_height="?android:attr/actionBarSize" />
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(“这里是Title”);
toolbar.setSubtitle(“这里是子标题”);
toolbar.setLogo(R.drawable.icon);
setSupportActionBar(toolbar);
4.nav_header_main.xml
4.nav_header_main.xml
0 0
- Android Studio的侧滑Demo
- Android Studio的使用--导入 API Demo
- Android Studio Demo
- Android Studio Demo
- Android Studio Wear Demo
- Android studio NDK demo
- Android Studio下使用Android Fragment的简单Demo(1)
- 【Android】Android Studio 快速打开Github上的Demo
- Android Studio NDK 入门demo
- Android Studio 2.2 JNI Demo
- android studio NDK demo详解
- android studio 集成TensorFlow Demo
- Android Studio NDK 入门demo
- 快速找到ANDROID STUDIO制作的demo(APK)
- 学习使用Android Studio编写AIDL的Demo
- Android Studio中 EventBus的使用demo(一)
- android studio 导入微信支付demo的一些问题
- Android AIDL demo (Android Studio)
- bzoj 2442: [Usaco2011 Open]修剪草坪 单调队列优化dp
- 关于解决安装ubuntu双系统中出现的不能识别磁盘分区、空闲空间变不可用问题
- 简单的waitpid函数的使用。
- virtualenv安装tensorflow
- Hibernate之基本配置测试用例
- Android Studio的侧滑Demo
- 勿以浮沙筑高台
- 旋转的风车—利用css3新属性
- JDK1.5--)很重要的特性——泛型
- http协议学习
- 算法总结(一) 算法分析
- Java基础 (==和equals)
- 小朋友排队
- UVA 455 Periodic Strings(字符串的循环节)