当前位置: 编程技术>移动开发
获取控件大小和设置调整控件的位置XY示例
来源: 互联网 发布时间:2014-10-19
本文导语: 网上很多人对设置控件的位置都使用view.setPadding(left, top, right, bottom) ,其实这玩意很差劲,它是设置自己本身位置的偏移,我们很少需要这种效果,我需要的设置控件相对屏幕左上角的X 、Y位置。众里寻他千百度,蓦然回首,...
网上很多人对设置控件的位置都使用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);
}
}
代码如下:
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);
}
}