您的位置:首页 > 移动开发 > Android开发

Android学习——FloatingActionButton制作简易弹出菜单

2018-02-16 02:05 513 查看
        又到了写博客的时候了,大过年的也算抽空复习了一下Android开发,今天来做个简单一点的弹出菜单,这里用到了FloatingActionButton,废话不多说直接上代码
        第一步:File--Project Structure--app--Dependencies里添加design依赖。
        第二步:修改XML为如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.drw.myapplication.MainActivity">
<!--这里添加了五个FloatingActionButton,id分别为start,first,second,third,forth,五个Button重叠,后面会写优化篇,现在这一次就直接这样了-->
<android.support.design.widget.FloatingActionButton
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
app:fabSize="normal"
android:layout_margin="10dp"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
app:fabSize="normal"
android:layout_margin="10dp"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
app:fabSize="normal"
android:layout_margin="10dp"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
app:fabSize="normal"
android:layout_margin="10dp"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/forth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
app:fabSize="normal"
android:layout_margin="10dp"/>
</RelativeLayout>
        第三步:修改MainActivity.java
public class MainActivity extends AppCompatActivity {
    //先声明五个FloatingActionButton
FloatingActionButton start;
FloatingActionButton first;
FloatingActionButton second;
FloatingActionButton third;
FloatingActionButton forth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=(FloatingActionButton)findViewById(R.id.start);
first=(FloatingActionButton)findViewById(R.id.first);
second=(FloatingActionButton)findViewById(R.id.second);
third=(FloatingActionButton)findViewById(R.id.third);
forth=(FloatingActionButton)findViewById(R.id.forth);
        //当点击start按钮时
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation_first=new TranslateAnimation(0,0,0,-300);//移动动画
animation_first.setInterpolator(new BounceInterpolator());//使用插值器
animation_first.setDuration(2000);//设置动画时间
animation_first.setFillAfter(true); //这里是设置动画结束后是否还在最后位置,false的话会回到原位
               first.startAnimation(animation_first);//开始动画,下面的都是基本重复的代码,后期会优化
Animation animation_second=new TranslateAnimation(0,140,0,-250);
animation_second.setInterpolator(new BounceInterpolator());
animation_second.setDuration(2000);
animation_second.setFillAfter(true);
second.startAnimation(animation_second);
Animation animation_third=new TranslateAnimation(0,250,0,-140);
animation_third.setFillAfter(true);
animation_third.setInterpolator(new BounceInterpolator());
animation_third.setDuration(2000);
third.startAnimation(animation_third);
Animation animation_forth=new TranslateAnimation(0,300,0,0);
animation_forth.setFillAfter(true);
animation_forth.setInterpolator(new BounceInterpolator());
animation_forth.setDuration(2000);
forth.startAnimation(animation_forth);
}
});
}
}
我是菜鸟,多多指教。。。DRW
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐