Android悬浮操作按钮示例教程

今天我们将学习关于 Android 浮动操作按钮。我们将讨论 FloatingActionButton,这是一个包含在 Material Design 指南中的新组件,以及SnackBar,它是 Toast 的 Material Design 替代品。

Android 浮动操作按钮

Android 浮动操作按钮用于突出显示屏幕上最重要的功能。这是一种引人注目的、时尚的方式来吸引用户的注意力。

Android 浮动操作按钮概述

要在我们的项目中使用 Material Design 小部件,我们需要在我们的 build.gradle 文件中编译以下依赖项,如下所示。

compile 'com.android.support:design:23.1.1'

浮动操作按钮小部件在 xml 布局中定义如下:

<android.support.design.widget.FloatingActionButton
      android:id="@+id/fab"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@android:drawable/ic_dialog_email"
      android:layout_gravity="bottom|end"
      app:elevation="6dp"
      app:pressedTranslationZ="12dp"/>

从上述定义的 xml 布局中得出的一些观察结果是:

  1. FloatingActionButton 扩展了 ImageView 类。这可以从定义的 android:src 属性中明显看出。
  2. 在上述 XML 布局中,`elevation` 属性用于在按钮上投射阴影,而 `pressedTranslationZ` 属性在按钮按下时使阴影增大。

A FloatingActionButton is placed within a CoordinatorLayout. A CoordinatorLayout helps facilitate interactions between views contained within it, which will be useful later to describe how to animate the button depending on scroll changes. SnackBar is a more enhanced widget when compared to a Toast. A SnackBar is invoked as follows:

Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

我们在另一个教程中详细讨论了 SnackBar重要提示:如果您一直在关注这些 Android 教程,您一定已经注意到随着新版本的构建工具升级至 23.1.1,新的空项目的项目结构已经发生了变化,并且上述提及的小部件默认存在于新的 Android Studio 项目中。因此,我们不再实现上述提及的小部件,而是快速浏览一下新项目结构。

Android 浮动操作按钮示例项目结构

如您所见,新增了一个名为 content_main.xml 的新 XML 布局文件。它与之前的 activity_main.xml 相同。

Android 浮动操作按钮示例

下面是新的activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.journaldev.floatingactionbutton.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.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

A toolbar is added by default as a replacement of an ActionBar. It’s added inside an AppBarLayout which is a direct child of CoordinatorLayout The AppBarLayout is used to achieve various scrolling behaviours such as collapse, flex space, and quick return. The MainActivity.java is defined as given below:

package com.journaldev.floatingactionbutton;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
//填充菜单;如果存在操作栏,则将项目添加到操作栏中。
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        
//在这里处理操作栏项目点击。只要您在AndroidManifest.xml中指定了父活动,操作栏就会
        
//自动处理对主页/上方按钮的点击。
        

        int id = item.getItemId();

        
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

A new attribute is added to the application tag inside the AndroidManifest.xml named android:supportsRtl="true". This enables right to left layouts in the application. Running this default application produces an output like below: As you can see, on clicking the floating action button, a SnackBar is displayed. This brings an end to this tutorial. You can create a new project in Android Studio and run it to see these features. Note: Make sure that you’re using the latest build tools. Reference: Android Reference Doc

Source:
https://www.digitalocean.com/community/tutorials/android-floating-action-button-example-tutorial