Floating Action Button(漂浮按钮)--谷歌新的设计包

效果图:

效果图

步骤一:

在build.gradle添加以下代码,导入包

dependencies {
    compile 'com.android.support:design:22.2.0'
}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

步骤二:

在xml文件中添加以下代码

<android.support.design.widget.FloatingActionButton
    android:id="@+id/bt_auction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/headerarrow"
    app:fabSize="normal" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

步骤三:

添加监听事件

FloatingActionButton  actionButton = (FloatingActionButton) findViewById(R.id.bt_auction);
actionButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
     //监听事件       
   }
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

注意点:

一:如何改变漂浮按钮颜色 
根据官方文档解释,并没有一个属性可以改变按钮颜色,这个按钮的颜色是取自项目的主题色,所以可以改变主题样式中的”colorAccent”值改变按钮颜色

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorAccent">#FF4081</item>
    </style>
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

二:如何改变按钮按下时的颜色 
在xml文件中添加以下属性

app:rippleColor="#ff0000"
  • 1
  • 1

三:如何改变按钮大小 
默认有2个尺寸可以选择”normal”和“mini”

app:fabSize="normal" 
  • 1
  • 1

节选自android开发者博客

posted @ 2016-12-30 15:01  天涯海角路  阅读(135)  评论(0)    收藏  举报