zoukankan      html  css  js  c++  java
  • android 获取控件大小和设置调整控件的位置XY

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


    import android.view.View;
    import android.view.ViewGroup.MarginLayoutParams;
    import android.widget.RelativeLayout;
    
    /*
     * 获取、设置控件信息
     */
    public class WidgetController {
    
    	/*
    	 * 获取控件宽
    	 */
    	public static int getWidth(View view)
    	{
    	    int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
            view.measure(w, h);
            return (view.getMeasuredWidth());       
    	}
    	/*
    	 * 获取控件高
    	 */
    	public static int getHeight(View view)
    	{
    	    int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
            view.measure(w, h);
            return (view.getMeasuredHeight());       
    	}
    	
    	/*
    	 * 设置控件所在的位置X,并且不改变宽高,
    	 * X为绝对位置,此时Y可能归0
    	 */
    	public static void setLayoutX(View view,int x)
    	{
    		MarginLayoutParams margin=new MarginLayoutParams(view.getLayoutParams());
    		margin.setMargins(x,margin.topMargin, x+margin.width, margin.bottomMargin);
    		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
    		view.setLayoutParams(layoutParams);
    	}
    	/*
    	 * 设置控件所在的位置Y,并且不改变宽高,
    	 * Y为绝对位置,此时X可能归0
    	 */
    	public static void setLayoutY(View view,int y)
    	{
    		MarginLayoutParams margin=new MarginLayoutParams(view.getLayoutParams());
    		margin.setMargins(margin.leftMargin,y, margin.rightMargin, y+margin.height);
    		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
    		view.setLayoutParams(layoutParams);
    	}
    	/*
    	 * 设置控件所在的位置YY,并且不改变宽高,
    	 * XY为绝对位置
    	 */
    	public static void setLayout(View view,int x,int y)
    	{
    		MarginLayoutParams margin=new MarginLayoutParams(view.getLayoutParams());
    		margin.setMargins(x,y, x+margin.width, y+margin.height);
    		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
    		view.setLayoutParams(layoutParams);
    	}
    }


  • 相关阅读:
    性能测试流程
    登录时获取验证码
    xpath在谷歌下的安装教程
    浏览器驱动安装
    Python3.3+Selenium3.0框架实战Web自动化测试实战
    selenium+python+unittest实现自动化测试(入门篇)
    python 3 的环境搭建Robot Framework
    canvas
    学习webpack
    开始学习typescript
  • 原文地址:https://www.cnblogs.com/xieyuan/p/3787406.html
Copyright © 2011-2022 走看看