经过一番研究,我发现在翻译后定义视图高度根本不会增加它的高度。 整个视图的高度总和似乎不能超过其父布局的高度。这意味着,如果您将父布局的高度设置为 MATCH_PARENT 并且您的屏幕尺寸为 960 dp,那么即使您定义了它的高度,您的子视图的最大高度也将是 960 dp,例如android:layout_height="1200dp".
因此,我决定动态调整父布局的高度,并将页脚布局的高度设为MATCH_PARENT。默认情况下,我的父布局的高度是MATCH_PARENT,但我在onCreateView() 上调用以下方法:
private void adjustParentHeight(){
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metrics);
ViewGroup.LayoutParams params = mView.getLayoutParams();
mFifthLineContainer.measure(0, 0);
params.height = metrics.heightPixels + (mFifthLineContainer.getMeasuredHeight() * 3);
mView.setLayoutParams(params);
}
这将使我的页脚布局脱离屏幕。然后我尝试使用View.animate().translationY(),但随后我遇到了另一个问题! Android 动画中存在一个错误在您调用时会导致闪烁 View.setY() on onAnimationEnd()。似乎原因是onAnimationEnd() 在动画真正结束之前被调用。以下是我用来解决此问题的参考资料:
Android Animation Flicker
Android Flicker when using Animation and onAnimationEnd Listener
因此,我改变了我的showBottomThreeLines() 方法:
private void showBottomThreeLines(boolean show){
if(show){
TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, -(mFifthLineContainer.getHeight() * 3), 0);
translateAnimation.setDuration(300);
translateAnimation.setFillAfter(true);
translateAnimation.setFillEnabled(true);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
mShiftContainer.setY(mShiftContainer.getY() + mFifthLineContainer.getHeight() * 3);
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mShiftContainer.startAnimation(translateAnimation);
} else{
TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, mFifthLineContainer.getHeight() * 3, 0);
translateAnimation.setDuration(300);
translateAnimation.setFillAfter(true);
translateAnimation.setFillEnabled(true);
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
mShiftContainer.setY(mFifthLineContainer.getY());
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mShiftContainer.startAnimation(translateAnimation);
}
}