新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android中FloatingActionButton实现悬浮按钮实例

Android中FloatingActionButton(悬浮按钮) 使用不是特别多,常规性APP应用中很少使用该控件. 当然他的使用方法其实很简单.直接上代码:

成都创新互联公司自2013年起,先为钟山等服务建站,钟山等地企业,进行企业商务咨询服务。为钟山企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

xml:

<?xml version="1.0" encoding="utf-8"?>


  

  

    

      

      
    

    

      

      
    

    

      

      
    
  

  

MainActivity:

package com.example.liupanpan.floatingactionbuttondemo;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  private CheckBox cbDelay;
  private FloatingActionButton fab01Add;
  private boolean isAdd = false;
  private RelativeLayout rlAddBill;
  private int[] llId = new int[]{R.id.ll01, R.id.ll02, R.id.ll03};
  private LinearLayout[] ll = new LinearLayout[llId.length];
  private int[] fabId = new int[]{R.id.miniFab01, R.id.miniFab02, R.id.miniFab03};
  private FloatingActionButton[] fab = new FloatingActionButton[fabId.length];
  private AnimatorSet addBillTranslate1;
  private AnimatorSet addBillTranslate2;
  private AnimatorSet addBillTranslate3;
  private AnimatorSet addBillTranslate4;
  private AnimatorSet addBillTranslate5;
  private AnimatorSet addBillTranslate6;

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

  private void initView() {
    cbDelay = (CheckBox) findViewById(R.id.cbDelay);
    fab01Add = (FloatingActionButton) findViewById(R.id.fab01Add);
    rlAddBill = (RelativeLayout) findViewById(R.id.rlAddBill);
    for (int i = 0; i < llId.length; i++) {
      ll[i] = (LinearLayout) findViewById(llId[i]);
    }
    for (int i = 0; i < fabId.length; i++) {
      fab[i] = (FloatingActionButton) findViewById(fabId[i]);
    }
  }

  private void setDefaultValues() {
    addBillTranslate1 = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.add_bill_anim);
    addBillTranslate2 = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.add_bill_anim);
    addBillTranslate3 = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.add_bill_anim);
//    addBillTranslate4 = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.add_bill_anim);
//    addBillTranslate5 = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.add_bill_anim);
//    addBillTranslate6 = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.add_bill_anim);
  }

  private void bindEvents() {
    fab01Add.setOnClickListener(this);
    for (int i = 0; i < fabId.length; i++) {
      fab[i].setOnClickListener(this);
    }
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.fab01Add:
        fab01Add.setImageResource(isAdd ? R.mipmap.ic_launcher_round : R.mipmap.ic_launcher_round);
        isAdd = !isAdd;
        rlAddBill.setVisibility(isAdd ? View.VISIBLE : View.GONE);
        if (isAdd) {
          addBillTranslate1.setTarget(ll[0]);
          addBillTranslate1.start();
          addBillTranslate2.setTarget(ll[1]);
          addBillTranslate2.setStartDelay(cbDelay.isChecked() ? 50 : 0);
          addBillTranslate2.start();
          addBillTranslate3.setTarget(ll[2]);
          addBillTranslate3.setStartDelay(cbDelay.isChecked() ? 100 : 0);
          addBillTranslate3.start();
//          addBillTranslate4.setTarget(ll[3]);
//          addBillTranslate4.setStartDelay(cbDelay.isChecked() ? 250 : 0);
//          addBillTranslate4.start();
//          addBillTranslate5.setTarget(ll[4]);
//          addBillTranslate5.setStartDelay(cbDelay.isChecked() ? 300 : 0);
//          addBillTranslate5.start();
//          addBillTranslate6.setTarget(ll[5]);
//          addBillTranslate6.setStartDelay(cbDelay.isChecked() ? 350 : 0);
//          addBillTranslate6.start();
        }
        break;
      case R.id.miniFab01:
        hideFABMenu();
        break;
      case R.id.miniFab02:

        hideFABMenu();
        break;
      case R.id.miniFab03:
        hideFABMenu();
        break;
      default:
        break;

    }
  }

  private void hideFABMenu() {
    rlAddBill.setVisibility(View.GONE);
    fab01Add.setImageResource(R.mipmap.ic_launcher_round);
    isAdd = false;
  }
}

运行程序,就可以实现当前的效果.问题XML的图片请自己替换一下,demo就不上传了.

效果如下:

Android中FloatingActionButton实现悬浮按钮实例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


分享标题:Android中FloatingActionButton实现悬浮按钮实例
文章URL:http://wjwzjz.com/article/ghijej.html
在线咨询
服务热线
服务热线:028-86922220
TOP