Snackbar的使用

在design包中新推了一个Snackbar的控件,这个控件和Toast控件非常类似,但是相比Toast这个控件提供了更多的交互性。在Snackbar中可以设置一个action,当点击这个action的时候,可以作出相应的响应。

<?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:id="@+id/rootview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.app.motion.materialdesginmotion.MainActivity">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingactionbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:padding="4dp"
        android:src="@drawable/ic_favorite_border_white_24dp"
        app:backgroundTint="@color/colorAccent"
        app:elevation="12dp"
        app:fabSize="normal"
        app:pressedTranslationZ="4dp"/>
</android.support.design.widget.CoordinatorLayout>
package com.app.motion.materialdesginmotion;

import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private FloatingActionButton mFloatingActionButton;
    private Snackbar mSnackbar;
    private CoordinatorLayout mCoordinatorLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCoordinatorLayout = (CoordinatorLayout) this.findViewById(R.id.rootview);
        mFloatingActionButton = (FloatingActionButton) this.findViewById(R.id.floatingactionbutton);
        mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mSnackbar.show();
            }
        });

        mSnackbar = Snackbar.make(mCoordinatorLayout, "click me", Snackbar.LENGTH_LONG);
        mSnackbar.setAction("UNDO", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "UNDO THIS", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

这里写图片描述

posted @ 2016-08-26 16:29  豌豆豆  阅读(68)  评论(0编辑  收藏  举报