FloatingActionButton
FloatingActionButton继承了ImageButton,是一种浮在UI上面的按钮。
简单使用
使用起来比较简单,和普通的按钮比起来差别并不是很大,基本使用都差不多,看起来更加炫一点。
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.manchester.androidsupportlibrary.FloatingActionButtonActivity">
<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="normal"
android:src="@android:drawable/ic_menu_add"
/>
<Button
android:layout_width="wrap_content"
android:id="@+id/hide"
android:text="hide"
android:textAllCaps="false"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:id="@+id/show"
android:textAllCaps="false"
android:text="show"
android:layout_height="wrap_content" />
</LinearLayout>
Activity
FloatingActionButton默认实现了hide和show方法,调用hide和show方法的时候可以传入一个监听对象,监听动画的执行状态。
public class FloatingActionButtonActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = FloatingActionButtonActivity.class.getSimpleName();
FloatingActionButton floatingActionButton;
Button hide;
Button show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_floating_action_button);
floatingActionButton = (FloatingActionButton) findViewById(R.id.floatingActionButton);
hide = (Button) findViewById(R.id.hide);
show = (Button) findViewById(R.id.show);
hide.setOnClickListener(this);
show.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.hide:
floatingActionButton.hide(new FloatingActionButton.OnVisibilityChangedListener() {
@Override
public void onShown(FloatingActionButton fab) {
super.onShown(fab);
Log.d(TAG, "onShown");
}
@Override
public void onHidden(FloatingActionButton fab) {
super.onHidden(fab);
Log.d(TAG, "onHidden");
}
});
break;
case R.id.show:
floatingActionButton.show(new FloatingActionButton.OnVisibilityChangedListener() {
@Override
public void onShown(FloatingActionButton fab) {
super.onShown(fab);
Log.d(TAG, "onShown");
}
@Override
public void onHidden(FloatingActionButton fab) {
super.onHidden(fab);
Log.d(TAG, "onHidden");
}
});
break;
}
}
}