注意:使用前需要添加Design依赖包,使用toolbar时需要隐藏标题头

FloatingActionButton

悬浮按钮:FloatingActionButton是重写ImageView的,所有FloatingActionButton拥有ImageView的一切属性。

  • app:backgroundTint - 设置FAB的背景颜色。
  • app:rippleColor - 设置FAB点击时的背景颜色。
  • app:borderWidth - 该属性尤为重要,如果不设置0dp,那么在4.1的sdk上FAB会显示为正方形,而且在5.0以后的sdk没有阴影效果。所以设置为borderWidth="0dp"。
  • app:elevation - 默认状态下FAB的阴影大小。
  • app:pressedTranslationZ - 点击时候FAB的阴影大小。
  • app:fabSize - 设置FAB的大小,该属性有两个值,分别为normal和mini,对应的FAB大小分别为56dp和40dp。
  • src - 设置FAB的图标,Google建议符合Design设计的该图标大小为24dp。
  • app:layout_anchor - 设置FAB的锚点,即以哪个控件为参照点设置位置。
  • app:layout_anchorGravity - 设置FAB相对锚点的位置,值有 bottom、center、right、left、top等。
    <android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom = "true"
android:layout_alignParentRight = "true"
android:layout_margin="20dp"
android:src="@mipmap/ic_launcher"/>

效果:

:

NavigationView

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

xml文件:

利用drawerLayout作为父布局,可以实现侧滑的功能

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="fanggao.qf.toolbar_drawerlayout_navigationview_floactionbar_snackbar.MainActivity">
<!--主界面-->
<RelativeLayout
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="wrap_content">
<!--自定义布局--> </android.support.v7.widget.Toolbar>
<!--悬浮的圆-->
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom = "true"
android:layout_alignParentRight = "true"
android:layout_margin="20dp"
android:src="@mipmap/ic_launcher"/>
</RelativeLayout>
<!--左侧菜单 android:layout_gravity = "start" 在布局的左侧-->
<android.support.design.widget.NavigationView
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity = "start"
app:headerLayout = "@layout/layout_header"
app:menu="@menu/menu"/> </android.support.v4.widget.DrawerLayout>

layout_header.xml

侧滑菜单中的标题头:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aaaaaa">
<ImageView
android:id="@+id/img_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELLO"
android:layout_below="@id/img_icon"
android:textSize="30sp"
android:layout_margin="20dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

注意:

 <!--有group就会有分割线了
android:checked="true" 在菜单项中图片就会显示为蓝色 false就会为灰色
所以会导致图片全部为灰色,解决办法:
方法1.在布局文件中添加app:itemIconTint="@color/blue"属性,表示设置图片的颜色全都为蓝色
方法2.在java代码中:NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view);
navigationView.setItemIconTintList(null); -->

menu.xml菜单布局

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- menu支持分组和子标题,但是子标题不支持icon-->
<!--单选-->
<group>
<item
android:title="激活"
android:icon="@mipmap/ic_launcher">
</item>
<item
android:title="会员"
android:icon="@mipmap/ic_launcher">
</item>
<item
android:title="钱包"
android:icon="@mipmap/ic_launcher">
</item>
</group>
<!--菜单-->
<item
android:title="选项"
android:icon="@mipmap/ic_launcher">
<menu>
<item
android:title="收藏"></item>
<item
android:title="好评"></item>
</menu> </item>
</menu>

main:

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
private DrawerLayout drawerLayout; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//设置主标题
toolbar.setTitle("标题");
//设置副标题
toolbar.setSubtitle("标题的子类");
//设置图标
toolbar.setLogo(R.mipmap.ic_launcher);
//设置Toolbar和DrawerLayout实现动画和联动
this.setSupportActionBar(toolbar); //左侧菜单与toolbar联动
// 给左上角图标的左边加上一个返回的图标
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
//ActionBarDrawerToggle可以监听抽屉开合事件,可以认为是DrawerListener的子类
//声明mDrawerToggle对象,其中R.string.open和R.string.close简单可以用"open"和"close"替代
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout,toolbar, R.string.open, R.string.close){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
Log.i("tag", "onDrawerOpened: ");
} @Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
Log.i("tag", "onDrawerClosed: ");
}
}; //使得图标和抽屉拉合保持同步
actionBarDrawerToggle.syncState();
//监听实现侧边栏的拉开和闭合,即抽屉drawer的闭合和打开
drawerLayout.addDrawerListener(actionBarDrawerToggle);
}
}

效果:

安卓Design包之NavigationView结合DrawerLayout,toolbar的使用,FloatingActionButton的更多相关文章

  1. 安卓Design包之CoordinatorLayout配合AppBarLayout,ToolBar,TabLaout的使用

    转载: CoordinatorLayout配合AppBarLayout,Toolbar和TabLayout的使用 控件的简单介绍: AppBarLayout:它是继承LinerLayout实现的一个V ...

  2. 安卓Design包之Toolbar控件的使用

    转自:ToolBar的使用 ToolBar的出现是为了替换之前的ActionBar的各种不灵活使用方式,相反,ToolBar的使用变得非常灵活,因为它可以让我们自由往里面添加子控件.低版本要使用的话, ...

  3. 安卓Design包之TabLayout控件的使用

    转自: 安卓Design包之TabLayout控件的简单使用 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android ...

  4. 安卓Design包之AppBar和Toolbar的联用

    前面讲了Design包的的CoordinatorLayout和SnackBar的混用,现在继续理解Design包的AppBar; AppBarLayout跟它的名字一样,把容器类的组件全部作为AppB ...

  5. 安卓Design包之超强控件CoordinatorLayout与SnackBar的简单使用

    在前面的Design中,学习使用了TabLayout,NavigationView与DrawerLayout实现的神奇效果,今天就带来本次Design包中我认为最有意义的控件CoordinatorLa ...

  6. 安卓Design包下的TextInputLayout和FloatingActionButton的简单使用

    终于介绍到Design包的最后的东西了. 也很简单,一个是TextInputLayout. TextInputLayout作为一个父容器,包含一个新的EditText,可以给EditText添加意想不 ...

  7. 安卓Design包之TabLayout控件的简单使用

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

  8. 安卓Design包之CollapsingToolbarLayout(可折叠的工具栏布局)的简单使用

    转自: CollapsingToolbarLayout的使用 注意:使用前需要添加Design依赖包,使用toolbar时需要隐藏标题头 CollapsingToolbarLayout作用是提供了一个 ...

  9. 安卓Design之NavigationView的使用

    前面讲解了Design包下的TabLayout的使用,下面将带来NavagationView和DrawLayout以及toolbar的联动. 项目已经同步至:https://github.com/na ...

随机推荐

  1. mysql repair data 语句

    1. 这条语句主要是用来修复一个表中的数据 , 在另一个统计表中没有统计时,进行修改统计表的操作 update expert_count ec inner join ( select sum(e.EF ...

  2. sql server 2014内存表

    内存数据库,指的是将数据库的数据放在内存中直接操作.相对于存放在磁盘上,内存的数据读写速度要高出很多,故可以提高应用的性能.微软的SQL Server 2014已于2014年4月1日正式发布,SQL ...

  3. mysql CASE WHEN的基础和多种用法

    CASE计算条件列表并返回多个可能结果表达式之一. CASE 具有两种格式: 简单 CASE 函数将某个表达式与一组简单表达式进行比较以确定结果. CASE 搜索函数计算一组布尔表达式以确定结果. 两 ...

  4. 无线路由器的“克隆MAC地址”是干什么作用的?

    本文章转载:http://blog.sina.com.cn/s/blog_4c900d100102uysb.html 1.问题: 无线路由器的“克隆MAC地址”是干什么作用的?怎样使用? 2.使用背景 ...

  5. VC++ 网络编程总结(一)

    1.套接字编程原理         一个完整的网间通信进程需要由两个进程组成,并且只能用同一种高层协议.也就是说,不可能通信的一段用TCP,而另一端用UDP.一个完整的网络信息需要一个五元组来标识:协 ...

  6. java泛型的讲解

    java泛型 什么是泛型? 泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指 ...

  7. Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp

    C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  8. C#程序实现动态调用DLL的研究[转]

    摘   要: 在< csdn 开发高手> 2004 年第 03 期中的<化功大法——将 DLL 嵌入 EXE >一文,介绍了如何把一个动态链接库作为一个资源嵌入到可执行文件,在 ...

  9. MKMapView的内存释放问题

    MKMapView的内存释放问题 by 伍雪颖 - (void)dealloc { self.mapView.showsUserLocation = NO; self.mapView.userTrac ...

  10. 调用webservice查询手机号码归属地信息

    Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务.在这里我们使用soap协议往webservice发送信息,然 ...