2022-02-09 Android setLayoutParams动态修改RelativeLayout控件layout_width、layout_marginTop等。实际应用实例:控件随手指移动。

一、修改大致方法如下:

LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) textview.getLayoutParams();  
    lp.leftMargin = 0;  
    textview.setLayoutParams(lp);  

二、实际应用实例:控件随手指移动。

    1、布局文件文件RelativeLayout中有个ImageVIew,id是code。

     2、首先我们要给控件设置触摸监听函数,监听按下,移动,抬起,移动等操作。

      3、在手指移动的过程中用setLayoutParams改变控件的位置以及大小。也可以用layout函数,但是这里我没有用。本例中手指上下移动的时候控件上下移动。手指向左移动的时候控件会变小,手指向右滑动的时候控件会变大。

ll_move.setOnTouchListener(new View.OnTouchListener() {
            private int startY;
            private int startX;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.e("打印操作:", "按下了");
                        //获取当前按下的坐标
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if(!is_move_to_modify)
                            break;
                        //获取移动后的坐标
                        int moveX = (int) event.getRawX();
                        int moveY = (int) event.getRawY();
                        //拿到手指移动距离的大小
                        int move_bigX = moveX - startX;
                        int move_bigY = moveY - startY;
                        //Toast.makeText(MainActivity.this,"\nX移动了" + move_bigX + "\nY移动了" + move_bigY, Toast.LENGTH_SHORT).show();
                        //拿到当前控件未移动的坐标
                        int left = ll_move.getLeft();
                        int top = ll_move.getTop();
                        //left += move_bigX;
                        top += move_bigY;
                        int right = left + ll_move.getWidth();
                        int bottom = top + ll_move.getHeight();
                       // ll_move.layout(left, top, right, bottom);
                        startX = moveX;
                        startY = moveY;
                        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) ll_move.getLayoutParams();
                        SharedPreferences sp = getSharedPreferences("health_code", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        //   String str_test_time_location= String.valueOf(test_time_location);
                        code_image_top = sp.getInt("code_image_top", 0);
                        code_image_top =code_image_top+move_bigY;
                        editor.putInt("code_image_top",code_image_top );
                        editor.commit();
                       // Toast.makeText(MainActivity.this,"lp.topMargin="+String.valueOf(lp.topMargin)+" move_bigY="+move_bigY+" ll_move.getTop()="+ll_move.getTop()+"code_image_top="+code_image_top, Toast.LENGTH_SHORT).show();
                        lp.topMargin=lp.topMargin+move_bigY;
                        ll_move.setLayoutParams(lp);
                        if(move_bigX > 30)
                        {
                            lp.height=lp.height+2;
                            lp.width=lp.width+2;
                            code_image_wh =lp.height;
                            editor.putInt("code_image_wh",code_image_wh );
                            editor.commit();
                            ll_move.setLayoutParams(lp);
                            Toast.makeText(MainActivity.this,"move right move_bigX "+move_bigX+"code_image_wh="+code_image_wh+"lp.height="+lp.height, Toast.LENGTH_SHORT).show();
                        }else if (move_bigX < -30)
                        {
                            lp.height=lp.height - 2;
                            lp.width=lp.width - 2;
                            code_image_wh =lp.height;
                            editor.putInt("code_image_wh",code_image_wh );
                            editor.commit();
                            ll_move.setLayoutParams(lp);
                            Toast.makeText(MainActivity.this,"move right move_bigX "+move_bigX+"code_image_wh="+code_image_wh+"lp.height="+lp.height, Toast.LENGTH_SHORT).show();
                        }

                        break;
                    case MotionEvent.ACTION_UP:
                        Log.e("打印操作:", "抬起了");
                        break;
                }
                return true;//此处一定要返回true,否则监听不生效
            }
        });

      4、涉及一点敏感的信息,这里就不上图。

四、参考文章

Android控件随手指的移动而移动_xiayiye5的博客-CSDN博客_android 随手指移动

Android 代码动态修改RelativeLayout布局 - 简书

Android动态设置Margin的方法 - 简书

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值