1,使用布局相对位置来设置,自己可以选择加上margin等属性条件,注意使用时要先清除之前的约束条件
private void switchContentViewRight() {
Log.i(TAG, "switchContentViewRight: ");
directionMode = Content_IN_RIGHT;
iv_menu.setBackground(getResources().getDrawable(R.drawable.slide_right_n));
RelativeLayout.LayoutParams containerlayoutParams = (RelativeLayout.LayoutParams) fragmentContainer.getLayoutParams();
containerlayoutParams.removeRule(RelativeLayout.ALIGN_PARENT_START);
fragmentContainer.setLayoutParams(containerlayoutParams);
containerlayoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
containerlayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
fragmentContainer.setLayoutParams(containerlayoutParams);
RelativeLayout.LayoutParams cardMenulayoutParams = (RelativeLayout.LayoutParams) cardMenu.getLayoutParams();
cardMenulayoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END);
cardMenu.setLayoutParams(cardMenulayoutParams);
cardMenulayoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
cardMenulayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
cardMenu.setLayoutParams(cardMenulayoutParams);
RelativeLayout.LayoutParams ivmenulayoutParams = (RelativeLayout.LayoutParams) iv_menu.getLayoutParams();
ivmenulayoutParams.addRule(RelativeLayout.END_OF, R.id.side_menu);
iv_menu.setLayoutParams(ivmenulayoutParams);
}
2,通过动画移位来实现
private void switchContentView(View leftView,View rightView, int direction) {
// 创建动画
ObjectAnimator leftAnimator = null;
ObjectAnimator rightAnimator = null;
ObjectAnimator ivSlideAnimator = null;
if (direction == Content_IN_LEFT) {
leftAnimator = ObjectAnimator.ofFloat(leftView, "translationX", 0).setDuration(300);
rightAnimator = ObjectAnimator.ofFloat(rightView, "translationX", 0).setDuration(300);
ivSlideAnimator = ObjectAnimator.ofFloat(iv_menu, "translationX", 0).setDuration(300);
} else if (direction == Content_IN_RIGHT) {
leftAnimator = ObjectAnimator.ofFloat(leftView, "translationX", leftView.getWidth()-getScreenWidth()).setDuration(300);
rightAnimator = ObjectAnimator.ofFloat(rightView, "translationX", getScreenWidth() - rightView.getWidth()).setDuration(300);
ivSlideAnimator = ObjectAnimator.ofFloat(iv_menu, "translationX", leftView.getWidth() - rightView.getWidth()).setDuration(300);
}
// 创建动画集
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(leftAnimator, rightAnimator, ivSlideAnimator);
animatorSet.start();
}
业务场景是左右布局的切换以及左右布局可以拉伸调整大小的占比,使用方法二遇到的问题是拉伸时都是往一个方向拉宽的,导致布局会超出屏幕范围,使用方法一可以使控件在不靠屏幕的一边拉伸