美文网首页
动态改变按钮位置

动态改变按钮位置

作者: 橙果子 | 来源:发表于2019-07-19 14:10 被阅读0次

    在Relativalayout中写了一排按钮, 需要左右移动位置, 就是将按钮从左边移动到右边, 再移动回去

    刚开始用了方法

    //allbtnLin 是需要移动的按钮
     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) allbtnLin.getLayoutParams();
                    if (!is_right) {
                        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                        allbtnLin.setLayoutParams(params); //使layout更新
                        is_right = true;
                    } else {
                        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                        allbtnLin.setLayoutParams(params); //使layout更新
                        is_right = false;
                    }
    

    发现问题横飞, 只能移动两次, 第三次就不好使了

    然后尝试直接用动画平移

     //获取屏幕宽度 px
            int screenWidth = DeviceUtils.getScreenWidth(getActivity());
            //获取按钮宽度 px
            int btnwidth = allbtnLin.getWidth();
            //移动的宽度 用屏幕宽-按钮宽
            int moveWodth = screenWidth - btnwidth;
    
            if (!is_right) {
                //平移到右边
                allbtnLin.animate().setDuration(400).translationX(moveWodth);
                is_right = true;
            } else {
                //回到左边的时候, 坐标设置为0
                allbtnLin.animate().setDuration(400).translationX(0);
                is_right = false;
            }
    

    推荐查看抛物线大神的hencoder属性动画讲解

    相关文章

      网友评论

          本文标题:动态改变按钮位置

          本文链接:https://www.haomeiwen.com/subject/qynjlctx.html