A lot of people on the Internet use View.setpadding (left, top, right, bottom) to set the control, which is really bad, it is the offset of the position itself, we rarely need this effect, I need to set the control relative to the top left-hand corner of the screen x, y position. The public find him 1100 degrees, suddenly looking back, that person is in the lights dim place!
Copy Code code as follows:
Import Android.view.View;
Import Android.view.ViewGroup.MarginLayoutParams;
Import Android.widget.RelativeLayout;
/*
* GET, set control information
*/
public class Widgetcontroller {
/*
* Get control width
*/
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 ());
}
/*
* Get Control high
*/
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 ());
}
/*
* Set the location of the control X, and do not change the width of the height,
* x is absolute position, at which point y may be 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);
}
/*
* Set the position of the control in Y, and do not change the width of the height,
* Y is an absolute position, at which point X may be 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);
}
/*
* Set the location of the control yy, and do not change the width of the height,
* XY is absolute position
*/
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);
}
}