Set the Android: gravity and Android: layout_gravity attributes in the program.

Source: Internet
Author: User

Reproduced from: http://blog.csdn.net/feng88724/article/details/6333809 Author: feng88724

Android: gravity and Android: layout_gravity attributes may be frequently used during UI layout.

Many people have explained the differences between these two attributes on the Internet. (Materials are from the Internet)

Linearlayout has two very similar attributes:

Android: gravity and Android: layout_gravity.

Their differences are:

Android: gravity attribute is a limitation on the content in this view. for example, text on a button. you can set the left-to-right position of the text relative to the view. android: layout_gravity is used to set the location of the view relative to the parent view. for example, if a button is in linearlayout, you can set it by placing it in linearlayout at the left or right position. android: gravity is used to set the alignment of the content in the view relative to the view component, while Android: layout_gravity is used to set the alignment of the view component relative to the container.

The principle is similar to Android: paddingleft and Android: layout_marginleft. If both attributes are set on the button.

Android: paddingleft = "30px" the content set on the button is 30 pixels away from the Left Border of the button.
Android: layout_marginleft = "30px" the entire button is 30 pixels away from the content set on the left.
Next, let's go back to the question. We can set Android: gravity = "center" to center the text in edittext in the edittext component. At the same time, we can set the android of edittext: layout_gravity = "right" to display the edittext component right in linearlayout. See the following results:

As we can see, in edittext, the text is displayed in the center, and the edittext component is aligned to the right of linearlayout.

Attached layout file:

<Linearlayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <edittext <br/> Android: layout_width = "wrap_content" <br/> Android: gravity = "center" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "one" <br/> Android: layout_gravity = "right"/> <br/> </linearlayout> 

The preceding settings are made by layout files ., I believe everyone has written this article. How can I set the component location through Java code?

The above results are still considered.

You can view the SDK and find a setgravity method. As the name suggests, this method is used to set the text alignment in the button component.

I did not find the setlayoutgravity method. However, if this method is available, how can we put the button in a iner that does not support the layout_gravity attribute!

As a result, this property may be in layout, So I carefully looked at the layoutparams of linearlayout and found that there is a gravity attribute in it, I believe this is used to set the position of the component relative to the container itself. That's right, it should be him.

After practice, I found that it is true that the code is attached. You can check the code yourself.

The code is relatively simple, but it took me some time to find them.

Button button = new button (this); <br/> button. settext ("one"); <br/> linearlayout. layoutparams Lp = new linearlayout. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); <br/> // This is equivalent to the Android: layout_gravity attribute in the layout file <br/> LP. gravity = gravity. right; <br/> button. setlayoutparams (LP); <br/> // This is equivalent to the Android: gravity attribute in the layout file <br/> button. setgravity (gravity. center); </P> <p> linearlayout Linear = new Li Nearlayout (this); <br/> // NOTE: For the linearlayout layout, it is necessary to set the horizontal or vertical layout! Otherwise, you will not be able to see the effect. <Br/> linear. setorientation (linearlayout. Vertical); <br/> linear. addview (button); <br/> setcontentview (linear ); 

Alternatively, you can:

 Button button = new button (this); <br/> button. settext ("one"); <br/> // This is equivalent to the Android: gravity attribute in the layout file <br/> button. setgravity (gravity. center); </P> <p> linearlayout Linear = new linearlayout (this); <br/> // note that for the linearlayout layout, it is necessary to set the horizontal or vertical layout! Otherwise, you will not be able to see the effect. <Br/> linear. setorientation (linearlayout. vertical); </P> <p> linearlayout. layoutparams Lp = new linearlayout. layoutparams (layoutparams. wrap_content, layoutparams. wrap_content); <br/> // This is equivalent to the Android: layout_gravity attribute in the layout file <br/> LP. gravity = gravity. right; </P> <p> linear. addview (button, LP); <br/> setcontentview (linear ); 

Okay, you won't be able to. It's the same as above. So much.

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.