在具有垂直方向的 LinearLayout 中绘制所有按钮。添加属性
android:visibility="gone"
单击主按钮时应出现的按钮。然后你可以在主按钮的 OnClickListener 中显示这些按钮:
button.setVisibility(View.VISIBLE);
其中 button 是对代码中布局的引用。
Button button = (Button) findViewById (R.id.your_button_id);
编辑:
要将动画添加到流程中,您必须向上/向下滑动出现的新按钮和下面的按钮。(将视图分组到布局中,以便更轻松地应用动画)。
这里有两个 XML 文件要在 res/anim 文件夹中创建:
slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="-50" android:toYDelta="0"
android:duration="300" />
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0" android:toYDelta="-50"
android:duration="300" />
在您的代码中创建动画:
Animation slideDown = AnimationUtils.loadAnimation(this, R.anim.slide_down);
并将其应用于按钮:
secondaryButton.startAnimation(slideDown);
向上滑动时,需要在动画结束后将可见性设置为“消失”,而不是之前。为此,您需要设置动画侦听器并隐藏 onAnimationEnd 中的按钮:
slideUp.setAnimationListener(new AnimationListener () {
@Override
public void onAnimationEnd(Animation animation) {
secondaryButton.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationStart(Animation animation) {}
});