Android--Java code settings margin

Source: Internet
Author: User

We can usually set the margin directly in XML, such as:

<android:layout_margin= "5dip"  android:src= "@drawable/image"  />

However, in some cases, it is necessary to write in Java code, but the view itself does not have a setmargin method, how to do?

By looking at the Android API, we found that Android.view.ViewGroup.MarginLayoutParams has a method SetMargins (left, top, right, bottom).

Its direct subclasses are: Framelayout.layoutparams, Linearlayout.layoutparams and Relativelayout.layoutparams.

How to use:

New linearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  Lp.setmargins (ten);  IMAGEVIEW.SETLAYOUTPARAMS (LP);

If this method has no effect, you can use the control to modify the margin, such as the previous ImageView, there is a GetLayout method, the layout obtained in the strong to linearlayout or other, and then set the margin.

I'm the dividing line of the king of the Land Tiger.

Reproduced

Android--Java code settings margin

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.