我会在这里使用 FrameLayout,如下所示:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/screen"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/top_panel"
android:layout_width="match_parent"
android:layout_height="@dimen/top_panel_height"
android:layout_gravity="bottom"/>
<FrameLayout
android:id="@+id/bottom_panel"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_panel_height"
android:layout_gravity="bottom"/>
</FrameLayout>
然后,为状态创建枚举
enum State {
HIDDEN {
@Override
public void moveTo(View topPanel, View bottomPanel, long animationDuration) {
topPanel.animate().translationY(topPanel.getHeight()).setDuration(animationDuration);
bottomPanel.animate().translationY(topPanel.getHeight() + bottomPanel.getHeight()).setDuration(animationDuration);
}
},
COLLAPSED {
@Override
public void moveTo(View topPanel, View bottomPanel, long animationDuration) {
topPanel.animate().translationY(0).setDuration(animationDuration);
bottomPanel.animate().translationY(bottomPanel.getHeight()).setDuration(animationDuration);
}
},
EXPANDED {
@Override
public void moveTo(View topPanel, View bottomPanel, long animationDuration) {
topPanel.animate().translationY(-bottomPanel.getHeight()).setDuration(animationDuration);
bottomPanel.animate().translationY(0).setDuration(animationDuration);
}
};
public abstract void moveTo(View topPanel, View bottomPanel, long animationDuration);
}
它的用法如下:
State newState = State.EXPANDED;
newState.moveTo(topPanel, bottomPanel, 200);