我自己回答这个问题。布局看起来像这样:
<RelativeLayout
android:id="@+id/pair_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@id/layout_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/list_background"
android:visibility="gone" >
<TextView
android:id="@id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</FrameLayout>
<FrameLayout
android:id="@id/layout_front"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background">
<Button
android:id="@id/button_blue"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</Button>
</FrameLayout>
</RelativeLayout>
后面的布局由一个 textview 组成,而前面的布局是一个按钮。单击按钮时,它将滑动到 textview 的底部。按钮监听器是这样实现的:
public class ButtonListener implements OnClickListener {
final FrameLayout l_front;
final FrameLayout l_back;
public blueListener(FrameLayout f, FrameLayout b) {
this.l_back = b;
this.l_front = f;
}
@Override
public void onClick(View v) {
l_back.setVisibility(View.VISIBLE);
l_back.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
l_front.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
TranslateAnimation tanim = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 0.0f,
TranslateAnimation.ABSOLUTE, 0.0f,
TranslateAnimation.ABSOLUTE, 0.0f,
TranslateAnimation.ABSOLUTE, l_back.getMeasuredHeight());
tanim.setDuration(400);
tanim.setFillAfter(true);
tanim.setInterpolator(new DecelerateInterpolator());
l_front.setAnimation(tanim);
((RelativeLayout)l_front.getParent()).getLayoutParams().height
= l_back.getMeasuredHeight() + l_front.getMeasuredHeight();
}
}