Get the control size and set the position of the sizing control XY sample _android

Source: Internet
Author: User
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);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.