android 获取控件大小和设置调整控件的位置XY

网上很多人对设置控件的位置都使用      view.setPadding(left, top, right, bottom) ,其实这玩意很差劲,它是设置自己本身位置的偏移,我们很少需要这种效果,我需要的设置控件相对屏幕左上角的X 、Y位置。众里寻他千百度,蓦然回首,那人却在灯火阑珊处!


[java]  view plain copy
  1. import android.view.View;  
  2. import android.view.ViewGroup.MarginLayoutParams;  
  3. import android.widget.RelativeLayout;  
  4.   
  5. /* 
  6.  * 获取、设置控件信息 
  7.  */  
  8. public class WidgetController {  
  9.   
  10.     /* 
  11.      * 获取控件宽 
  12.      */  
  13.     public static int getWidth(View view)  
  14.     {  
  15.         int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
  16.         int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
  17.         view.measure(w, h);  
  18.         return (view.getMeasuredWidth());         
  19.     }  
  20.     /* 
  21.      * 获取控件高 
  22.      */  
  23.     public static int getHeight(View view)  
  24.     {  
  25.         int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
  26.         int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
  27.         view.measure(w, h);  
  28.         return (view.getMeasuredHeight());         
  29.     }  
  30.       
  31.     /* 
  32.      * 设置控件所在的位置X,并且不改变宽高, 
  33.      * X为绝对位置,此时Y可能归0 
  34.      */  
  35.     public static void setLayoutX(View view,int x)  
  36.     {  
  37.         MarginLayoutParams margin=new MarginLayoutParams(view.getLayoutParams());  
  38.         margin.setMargins(x,margin.topMargin, x+margin.width, margin.bottomMargin);  
  39.         RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);  
  40.         view.setLayoutParams(layoutParams);  
  41.     }  
  42.     /* 
  43.      * 设置控件所在的位置Y,并且不改变宽高, 
  44.      * Y为绝对位置,此时X可能归0 
  45.      */  
  46.     public static void setLayoutY(View view,int y)  
  47.     {  
  48.         MarginLayoutParams margin=new MarginLayoutParams(view.getLayoutParams());  
  49.         margin.setMargins(margin.leftMargin,y, margin.rightMargin, y+margin.height);  
  50.         RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);  
  51.         view.setLayoutParams(layoutParams);  
  52.     }  
  53.     /* 
  54.      * 设置控件所在的位置YY,并且不改变宽高, 
  55.      * XY为绝对位置 
  56.      */  
  57.     public static void setLayout(View view,int x,int y)  
  58.     {  
  59.         MarginLayoutParams margin=new MarginLayoutParams(view.getLayoutParams());  
  60.         margin.setMargins(x,y, x+margin.width, y+margin.height);  
  61.         RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);  
  62.         view.setLayoutParams(layoutParams);  
  63.     }  
  64. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值