Android and android Official Website

Source: Internet
Author: User

Android and android Official Website

We can usually set margin directly in xml, such:

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

However, in some cases, it needs to be written in java code, but the View itself does not have the setMargin method. What should I do?

After reading the android api, we found that android. view. ViewGroup. MarginLayoutParams has a setMargins (left, top, right, bottom) method ).

Its direct subclasses include: FrameLayout. LayoutParams, LinearLayout. LayoutParams and RelativeLayout. LayoutParams.

Usage:

LinearLayout. layoutParams lp = new LinearLayout. layoutParams (LinearLayout. layoutParams. WRAP_CONTENT, LinearLayout. layoutParams. WRAP_CONTENT); lp. setMargins (10, 20, 30, 40); imageView. setLayoutParams (lp );

If this method does not work, you can use the control that needs to modify margin, such as the previous imageView. There is a getLayout method, and the obtained layout can be strongly converted to LinearLayout or another type, then set the margin.

I am the dividing line of tiantiao

 

 

Reprinted

Related Article

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.