Android 悬浮按钮 FloatingActionButton 和交互提示 SnackBar

Android Material Design 之悬浮按钮FloatingActionButton、提示工具SnackBar、CoordinatorLayout布局 悬浮按钮FloatingActionButton和提示工具SnackBar是Design Support提供的控件。 悬浮按钮FloatingActionButton及其点击事件: 悬浮按钮FloatingActionButton是不属于主界面平面上的控件,而是位于另一个维度,好像三维坐标上的Z轴。设置它的 app:elevation=""可以设置悬浮高度,值越大,悬浮高,投影大。同时也有... 阅读详情

      今天是我们分享的关于 Material Design 的第五篇文章了,今天给大家简单分享 FloatingActionButton 和 Snackbar 这两个 Material Design 小控件用法,通过之前几篇博文的相关的对 Material Design 的介绍,大家对 Material Design 也有了一些了解,本篇我们来接着学习上面说到的两个简单的小控件,FloatingActionButton 其实就是我们看到的悬浮按钮,SnackBar 则是底部弹出的一个小型交互控件,接下来我们开始来演绎


一、悬浮按钮 FloatingActionButton


这里我们直接上 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"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#c7c6c6">

    <FrameLayout
        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="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:titleTextColor="#f5f0f0" />

        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="18dp"
            android:elevation="20dp"
            android:src="@mipmap/pen"
            app:backgroundTint="#0882f4" />


    </FrameLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        app:headerLayout="@layout/navigation_head"
        app:menu="@menu/navigation">

    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

       这里我们是接着上一篇的博文代码来敲的,相信看过上一篇的大家也很明白,不过没有看过的也不要紧,这里我们只是在主布局中增加了 FlaotingActionButton,即今天我们要实现的功能,这里需要注意的就是,FlaotingActionButton 按钮会默认使用 colorAccent 来作为按钮的颜色,如果要更改的话也可以通过 app:backgroundTint 属性来设置,这里我们通过 layout_gravity 把悬浮按钮设置到右下角,然后通过 layout_margin 属性给控件四周留点距离,最后通过 src 属性来设置了图标,就这么简单,我们就实现了,效果图如下:




       这样悬浮按钮就在右下方出现了,仔细看会发现按钮下面还有阴影,这也是 Material Design 设计的风格体现,另外我们还可以通过 app:elevation 来指定一个高度值,高度值越大,投影范围也越大,高度值越小,投影范围也越小,投影的效果越浓,当然这些效果都不是特别的明显,接下来我们来看如何实现 FlaotingActionButton 的点击事件


MainActivity 中的代码如下:

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;

    private NavigationView navigationView;

    private FloatingActionButton floatingActionButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
        setSupportActionBar(toolbar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

        navigationView = (NavigationView) findViewById(R.id.navigation);

        // FloatingActionButton 悬浮按钮
        floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.mipmap.category);
        }

        navigationView.setCheckedItem(R.id.account);

        navigationItemSelected();

        floatingActionButton();
    }

    private void navigationItemSelected() {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                mDrawerLayout.closeDrawers();

                //do something

                return true;
            }
        });
    }

    /**
     * FloatingActionButton 响应
     */
    private void floatingActionButton() {
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(MainActivity.this, "FloatingActionButton", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.backup:
                Toast.makeText(this, "Click Backup", Toast.LENGTH_LONG).show();
                break;
            case R.id.delete:
                Toast.makeText(this, "Click Delete", Toast.LENGTH_LONG).show();
                break;
            case R.id.setting:
                Toast.makeText(this, "Click Setting", Toast.LENGTH_LONG).show();
                break;

            case android.R.id.home:
                mDrawerLayout.openDrawer(Gravity.LEFT);
                break;
            default:
        }
        return true;
    }
}

       这里的代码也是挺清晰的,首先我们先初始化 FlaotingActionButton 控件,然后通过 setOnclickListener 来设置点击事件,这里是不是太熟悉了,是的,FlaotingActionButton 的点击事件的设置是和我们普通的 Button 一模一样,并没有什么特别之处,这里我们是点击按钮弹出一个吐司,大家可以根据业务的具体需要进行相应的功能实现


二、交互提示 Snackbar


       上面我们已经掌握了 FlaotingActionButton 的基本用法,现在我们来学习 Material Design 的另一个小工具 Snackbar,这里首先要明确 Snackbar 并不是 Toast 的替代品,应用场景是不一样的,Toast 只是一个提示作用,用户并不能进行操作,而 Snackbar 则不同,它允许在提示当中加入一个交互按钮,当用户点击的时候可以进行一写相应额逻辑操作,相应的提升了用户体验


修改 Activity 中的代码如下:

    /**
     * FloatingActionButton 响应
     */
    private void floatingActionButton() {
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Snackbar.make(v, "data", Snackbar.LENGTH_SHORT).setAction("你好", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //do something
                        Toast.makeText(MainActivity.this, "data", Toast.LENGTH_SHORT).show();
                    }
                }).show();
            }
        });
    }

       我们来进行简单的分析,这里调用了 Snackbar 的 make() 方法来创建一个 Snackbar 对象,make() 方法的第一个参数要出入一个View,Snackbar 会使用这个 View 来自动查找最外层的布局,用于展示 Snackbar,第二个参数是 Snackbar 中显示的内容,第三个参数是显示 Snackbar 的时间长短,这里和 Toast 设置时间的长短类似,接着调用 setAction() 方法来设置一个动作,从而让 Snackbar 不仅仅是一个提示,而是可以和用户进行交互,这个是最重要的,效果图如下:




       可以看到这里出现了一个不好的体验,就是 Snackbar 从底部弹出以后遮挡了 FlaotingActionButton 按钮,那么这个该如何解决呢,其实也很简单,就是通过 CoordinatorLayout 布局,我们具体来看一下,我们修改 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"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#c7c6c6">

    <android.support.design.widget.CoordinatorLayout
        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="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:titleTextColor="#f5f0f0" />

        <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="18dp"
            android:elevation="20dp"
            android:src="@mipmap/pen"
            app:backgroundTint="#0882f4" />


    </android.support.design.widget.CoordinatorLayout>

这里我们把 FrameLayout 换成了 CoordinatorLayout 布局,我们再运行来看一下效果图:



   

        这里看到上面的问题就很好的解决了,是不是很神奇,CoordinatorLayout 其实就是一个加强版的 FrameLayout ,这个布局是由Material Design 库提供的,CoordinatorLayout 可以监听所有子控件的各种事件,然后做出最为合理的响应,例如我们刚才的使用,Snackbar 提示将悬浮按钮挡住了,而如果我们能够监听到 Sanckbar 的弹出事件,则把按钮向上偏移,从而确保 FlaotingActionButton不会被挡住,另外由于是加强版的 FrameLayout 大家可以放心使用,不会有任何的副作用,好了今天我们就介绍到这里,今天的内容不多,但确是一些实用的小技巧,希望大家可以掌握


【Android】ToolBar,滑动菜单,悬浮按钮和可交互提示等的使用方法 Toolbar的强大之处在于,它不仅继承了 ActionBar的所有功能,而且灵活性很高,可以配合其他控件来完成一些Material Design的效果。任何一个新建的项目,默认都是会显示ActionBar。 阅读详情

相关推荐

Android使用Material Design实现悬浮按钮

前言 ​ 本文是对《Android第一行代码》第十二章《最佳UI体验——Material Design实战》中关于悬浮按钮实现的学习和记录,主要内容包括—— FloatingActionButton控件 Snackbar控件 CoordinatorLayout布局 其实关于悬浮按钮的实现十分简单,Design Support库中做了大量的封装,只需要运用以上3个基础控件就可以完成一个最简单的...

大山喵写博客的地方 1688

FloatingActionButton

FloatingActionButton(FAB)是 Android 5.0 新特性——Material Design中的一个控件。FloatingActionButton其实由3个单词组成, Floating:悬浮;Action:行为,Button:按钮。的确,FAB就是一个悬浮的按钮。 本文将结合笔者的开发经验,通过FAB的基本使用,使用过程中遇到的问题及解决方案等方面进行阐述,以便大家进一步认识FAB。 另外,本文若无特别说明,源码分析均采用27.1.0。 一.基本使用 1.FAB是Mater

悟心的专栏 6279

【Android笔记99】Android之悬浮按钮FloatingActionButton、Snackbar、CardView卡片式布局

app:fabSize:设置悬浮按钮的大小,三个取值:auto、mini、normal。app:backgroundTint:设置悬浮按钮的背景颜色。app:rippleColor:设置悬浮按钮,点击时候,产生的涟漪颜色,也就是按钮点击时候会变成另外一种颜色。app:elevation:按钮悬浮的高度,高度越大,按钮显示的阴影越深。android:src:按钮显示的图标。android:layout_gravity:设置悬浮按钮显示的位置,

✅ Java 开发工程师 1247

Android FloatingActionButton(浮动动作按钮的动画 ) 使用详情

浮动动作按钮(FAB)已经成为最简单的组件之一,成为设计师和开发人员快速和必不可少的最爱。家人们,这篇文章将向您展示如何使您的应用程序FAB交互式,以及如何制作自己的动画。但是让我们从简单的开始,将浮动操作按钮添加到一个Android项目中。浮动动作按钮看起来像这样的布局文件,如果创建一个空白活动的项目,将自动生成;

向着太阳迎着光 6366

基于车牌识别的停车收费系统的设计与实现(源码+开题)

本系统(程序源码)带文档lw万字以上文末可获取一份本项目的java源码和数据库参考。系统程序文件列表开题报告内容研究背景: 随着城市化进程的加速和汽车保有量的增加,停车难成为了一个普遍存在的问题。传统的停车收费方式存在着效率低下、管理混乱等问题。因此,开发一款基于车牌识别的停车收费系统具有重要的现实意义。研究意义: 基于车牌识别的停车收费系统可以有效地解决传统停车收费方式在效率和准确性方面的问题,提高停车服务的效率和质量。

迟限程序设计 1392

Cadence仿真笔记(二):传统noise仿真—共源极的噪声

cadence中进行传统噪声仿真

Bao_xue的博客 1万+

Android——FloatingActionButton使用说明以及避坑

FloatingActionButton-使用说明以及避坑,中心图标背景颜色无法改变问题

5086

AMEsim柱塞泵的建模学习

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言一、为什么要对柱塞泵进行仿真?二、Amesim中柱塞泵的模型1.amesim中自己的定排量五柱塞的斜盘柱塞泵2.压力、流量仿真总结 前言 最近在学习Amesim进行柱塞泵的仿真学习,顺便在这里记录我的学习过程,这是我的初衷 提示:以下是本篇文章正文内容,下面案例可供参考 一、为什么要对柱塞泵进行仿真? 柱塞泵是功能和动力学比较复杂的流体机械。而在实际实验中,往往没有条件或者需要对柱塞泵做一些优化,所以需要我们利用仿真...

innocent_cft的博客 8184

Android FloatingActionButton控件

1. FloatingActionButton类 FloatingActionButton是5.0版本出现的控件,显示一个圆形悬浮按钮。需要添加Design依赖库,并且使用Theme.AppCompat主题。 2. FloatingActionButton配置 FloatingActionButton的配置 android:src,显示的图标,最好是24dp的 app:backgro...

假装你是大灰狼的专栏 8146

C# & SQL Server 实时数据同步:7步轻松搞定,代码详解

实时数据同步是指在一个数据库中的数据发生变化时,能够立即或近乎立即地将这些变化同步到另一个数据库中。这种同步可以是单向的(从源数据库同步到目标数据库),也可以是双向的(两个数据库互相同步)。通过本文的详细讲解和代码示例,相信你已经学会了如何使用C#和SQL Server实现数据库的实时数据同步。实时数据同步不仅可以提升系统的响应速度,还能确保数据的一致性和准确性。希望这篇教程对你有所帮助,期待你在C#编程中取得更大的成就!🌟✨如果你有任何问题或建议,欢迎在评论区留言交流!

java专栏 514

图神经网络实战(6)——使用PyTorch构建图神经网络

图数据集通常比简单的连接集合更加丰富,因为节点和边可以具有表示分数、颜色、单词等特征。包含这些额外信息在输入数据中对于生成最佳嵌入至关重要。在本节中,我们将使用 Cora 和 Facebook Page-Page 数据集,首先将它们视为表格数据集,观察香草神经网络在节点特征上的表现如何。然后,尝试在神经网络中加入拓扑信息,实现图神经网络 (Graph Neural Networks, GNN) 架构:一个同时考虑节点特征和边的简单模型。最后,我们将比较两种架构的性能。

盼小辉丶的博客 2万+

Android开发--FloatingActionButton/Snackbar/CoordinatorLayout

FloatingActionButton悬浮按钮是support库中的按钮因此布局文件如下 <!--增加悬浮按钮--> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout

海神九考 602

k230+CanMV+micropython环境下byte、int、hex、string之间的转换常用函数实测

【代码】k230+CanMV+micropython环境下byte、int、hex、string之间的转换常用函数实测。

weixin_47446386的博客 119

Android 学习之《第一行代码》第二版 笔记(二十二)Material Design 实战 —— 悬浮按钮和可交互提示

实现基础: Android 学习之《第一行代码》第二版 笔记(二十一)Material Design 实战 —— Toolbar 和 滑动菜单 一、悬浮按钮(FloatingActionButton) FloatingActionButton 是Design Support 库中提供的一个控件,可帮助我们比较轻松地实现悬浮按钮的效果,默认使用colorAccent 来作为按钮的颜色。 1. 效果图...

Nicholas_hzf的博客 466

【GameFramework框架内置模块】15、场景(Scene)

场景(Scene)模块,提供场景管理的功能,负责场景的加载、卸载、切换和管理。可以方便地管理不同场景之间的切换以及不同场景之间的信息传递。

优快云博客专家,Unity3D高级软件工程师,《Unity 3D从入门到实战》作者。 1886

【SpringBoot 3.x 第56节】再见 Feign?原生 HTTP 接口强势上位,这才是微服务调用的正确姿势!

🏆本文收录于《滚雪球学SpringBoot 3》,专门攻坚指数提升,本年度国内最系统+最专业+最详细(永久更新)。    本专栏致力打造最硬核 SpringBoot3 从零基础到进阶系列学习内容,🚀均为全网独家首发,打造精品专栏,专栏持续更新中…欢迎大家订阅持续学习。 如果想快速定位学习,可以看这篇【SpringBoot3教程导航帖】,你想学习的都被收集在内,快速投入学习!!两不误。    若还想学习更多,可直接前往《滚雪球学SpringBoot(全版本合集)》,涵盖SpringBoot所有版本教学文章

**My Coding Family** 1017

Android【Toolbar、DrawerLayout 滑动菜单、 FloatingActionButton 悬浮按钮、Snackbar可交互提示、 Coordinator】

Snackbar允许在提示中加入一个可交互按钮,当用户点击按钮的时候,就可以执行一 些额外的逻辑操作。CoordinatorLayout会监听所有子控件的各种事件,并且自动帮助我们做出最为合理的 响应。在项目运行在虚拟机上时,都可以看见屏幕上方有一个栏目(下图红框圈出的部分),此为系统默认的。以便于我们更灵活的编辑栏目内容,所以我们先将主题文件中的。弹出的时候会把悬浮按钮遮住,但是这个问题我们可以用。,再次运行就可以发现顶端的栏目不见了。,他们有不同的应用场景。.

weixin_63261150的博客 1627

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Android实现悬浮按钮的合理的可交互提示

Android实现悬浮按钮的合理的可交互提示使用FloatingActionButton,Snackbar,CoordinatorLaout的合并使用。这是三个分别是悬浮按钮控件、可交互提示、能监听布局中所有控件并作出合理响应的布局。 Android实现悬浮按钮的合理的可交互提示的实现 1.悬浮按钮的实现FloatingActionButton &lt;android.support.de...

JunLi_YNU的博客 800

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

第一行代码Android 阅读笔记 第十二章(仅自用)

自学笔记

weixin_51109304的博客 241

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

android 悬浮按钮和可交互提示,android ——悬浮按钮及可交互提示

一、悬浮按钮FloatingActionButton是Design Support中的一个控件,它会默认colorAccent作为按钮的颜色,还可以给按钮一个图标。这是没有图标的,这是有图标的。然后布局文件这样写:layout_gravity的属性值bottomb表示在顶部,end会根据系统确定是在左下还是右下。elevation表示按钮的悬浮高度,属性值越大,阴影就越小。。。其实区别并不大,平时...

weixin_31368913的博客 1083

android 悬浮按钮和可交互提示,android ——悬浮按钮及可交互提示(示例代码)

一、悬浮按钮FloatingActionButton是Design Support中的一个控件,它会默认colorAccent作为按钮的颜色,还可以给按钮一个图标。这是没有图标的,这是有图标的。然后布局文件这样写:layout_gravity的属性值bottomb表示在顶部,end会根据系统确定是在左下还是右下。elevation表示按钮的悬浮高度,属性值越大,阴影就越小。。。其实区别并不大,平时...

weixin_39867142的博客 221

悬浮按钮和可交互提示的使用

自定义标题栏悬浮按钮/>滑动自定义菜单。

2501_90404051的博客 712

android 布局收缩成球,Android使用Material Design实现悬浮按钮

前言​ 本文是对《Android第一行代码》第十二章《最佳UI体验——Material Design实战》中关于悬浮按钮实现的学习和记录,主要内容包括——FloatingActionButton控件Snackbar控件CoordinatorLayout布局其实关于悬浮按钮的实现十分简单,Design Support库中做了大量的封装,只需要运用以上3个基础控件就可以完成一个最简单的悬浮球样式了。实...

weixin_30562541的博客 481

Toolbar(自定义标题栏),DrawerLayout 滑动菜单, NavigationView, FloatingActionButton 悬浮按钮和可交互提示, CoordinatorLayou

Toolbar(自定义标题栏),DrawerLayout 滑动菜单, NavigationView, FloatingActionButton 悬浮按钮和可交互提示, CoordinatorLayou

jzj_nd的博客 750

android 悬浮按钮

这里介绍一下悬浮按钮-----FloatingtActionButton的实现,以及会出现的问题。     布局文件activity_main.xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk

一个北漂的程序猿 1万+
上一篇: Android 用 TabLayout + ViewPager + Fragment 实现顶部、底部导航栏
下一篇: Android 开发的未来语言 Kotlin 及学习资源
QDJdeveloper
博客等级 码龄11年 108粉丝 49原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值