FloatingActionButton在github上的项目主页是:https://github.com/makovkastar/FloatingActionButton
它的依赖包NineOldAndroids在github上的项目主页是:https://github.com/JakeWharton/NineOldAndroids
(导包的时候注意要把android-support-v7-appcompat作为它的一个Library,不要重复了NineOldAndroids的引用)
测试代码:
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zzw.testfloatingactionbutton.MainActivity" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.melnykov.fab.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="16dip"
android:src="@drawable/ic_launcher"
fab:fab_shadow="true"
fab:fab_type="normal" />
</RelativeLayout>
MainActivity.java:
package com.zzw.testfloatingactionbutton;
import com.melnykov.fab.FloatingActionButton;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] data=new String[100];
for(int i=0;i<data.length;i++){
data[i]="ZZW1994"+i;
}
ListView listView=(ListView) findViewById(R.id.listView);
ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);
listView.setAdapter(adapter);
//附着在ListView,跟随ListView滚动滑入滑出
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.attachToListView(listView);
fab.setColorPressed(0xffb71c1c);
fab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "hello", 0).show();
}
});
}
}