内容修改了一下,可能不是好,功能应该可以。
1、activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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=".MainActivity">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:background="#C3B8B8"
android:gravity="center"
android:text="@string/setupthe_title"
android:textColor="#0B0A0A"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
2、MainActivity.java
public class MainActivity extends AppCompatActivity {
LinearLayout linearlayout;
FloatingActionButton floatingActionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearlayout = (LinearLayout)findViewById(R.id.linearlayout);
floatingActionButton = (FloatingActionButton)findViewById(R.id.fab);
floatingActionButton.setTag(0);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(R.id.fab==view.getId()){
if((int)(floatingActionButton.getTag())==0){
floatingActionButton.setTag(1);
//收起布局
visibleAnimator(linearlayout);
}else {
floatingActionButton.setTag(0);
//展示布局
invisibleAnimator(linearlayout);
}
}
}
});
}
//收起动画
public static void visibleAnimator(final View view) {
if (view != null) {
int viewHeight = view.getHeight();
if (viewHeight == 0) {
int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(width, height);
viewHeight = view.getMeasuredHeight();
}
ValueAnimator animator = ValueAnimator.ofInt(0, viewHeight);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = (int) animation.getAnimatedValue();
view.setLayoutParams(params);
}
});
animator.start();
}
}
//展示动画
public static void invisibleAnimator(final View view) {
if (view != null) {
int viewHeight = view.getHeight();
if (viewHeight == 0) {
int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(width, height);
viewHeight = view.getMeasuredHeight();
}
ValueAnimator animator = ValueAnimator.ofInt(viewHeight, 0);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = (int) animation.getAnimatedValue();
view.setLayoutParams(params);
}
});
animator.start();
}
}
}