动态设置布局位置方法

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();
    }

业务场景是左右布局的切换以及左右布局可以拉伸调整大小的占比,使用方法二遇到的问题是拉伸时都是往一个方向拉宽的,导致布局会超出屏幕范围,使用方法一可以使控件在不靠屏幕的一边拉伸

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值