"Android Layout" set Android:gravity and Android:layout_gravity properties in your program

Source: Internet
Author: User

The Android:gravity and Android:layout_gravity properties may often be used in the UI layout.

On the difference between these two attributes, there are already many people on the internet to explain, this way to briefly say. (Information from the network)

The LinearLayout has two very similar properties:

Android:gravity and Android:layout_gravity.

The difference between them is:

The Android:gravity property is the qualification for the content in the view. For example, a button above the text. You can set the text to the left, right, and so on, relative to the view.

Android:layout_gravity is used to set the location of the view relative to the parent view. For example, a button in the LinearLayout, you want to put the button in the LinearLayout left, on the right and so on can be set through this property.

That is, android:gravity is used to set the alignment of content in view relative to the view component, while android:layout_gravity is used to set the alignment of the view component with respect to the container.

The principle is a bit similar to Android:paddingleft and Android:layout_marginleft. Assume that both properties are set at the same time on the button.

Android:paddingleft= the content set on the 30px button is 30 pixels from the left edge of the button
Android:layout_marginleft= "30px" The entire button is set from the left side of the content 30 pixels


Back to the point, we can set the android:gravity= "center" to let the text in the EditText in the EditText component, the same time we set EditText android:layout_gravity= "Right" to make the EditText component appear in the LinearLayout. Look at the effect:

As we have shown, in EditText, the text is centered, and the EditText component itself is aligned to the right side of the linearlayout.

Attach the layout file:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" an Droid:layout_width= "Fill_parent" android:layout_height= "fill_parent" > <edittext android:layout_width= "w Rap_content "android:gravity=" center "android:layout_height=" Wrap_content "android:text=" one " Android:layout_gravity= "Right"/></linearlayout>

Then the above is set by the way the layout file. , I believe everyone has written, so how to use Java code to set the location of the component?

Still consider achieving the above effect.

By looking at the SDK, there is a setgravity method that, as the name implies, is the way to set the alignment of the text in the button component.

Meticulous find a lap, did not find Setlayoutgravity method, a little disappointed. Just think about it, assuming that this way, put the button in the container that does not support the Layout_gravity property is good!

So thought, this attribute may be in layout, so carefully looked at LinearLayout Layoutparams, sure enough to find, there is a gravity attribute, I believe this is used to set the component relative to the position of the container itself, yes, it should be him.

After practice, it is found that, if so, attach the code, and you see for yourselves.

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

button button = New button (this); Button.settext ("one"); Linearlayout.layoutparams LP = new Linearlayout.layoutparams (layoutparams.wrap_content,layoutparams.wrap_content) ;//Here is equivalent to the android:layout_gravity attribute in the layout file Lp.gravity = GRAVITY.RIGHT;BUTTON.SETLAYOUTPARAMS (LP);// This is equivalent to the android:gravity attribute button.setgravity (gravity.center) in the layout file. LinearLayout linear = new LinearLayout (this);//Note that for the LinearLayout layout, it is necessary to set landscape or portrait! Otherwise you will not see the effect. Linear.setorientation (linearlayout.vertical); Linear.addview (button); Setcontentview (linear) ;

Or it can:

button button = New button (this), Button.settext ("one"),//This is equivalent to the Android:gravity property button.setgravity in the layout file ( Gravity.center); LinearLayout linear = new LinearLayout (this);//Note that for the LinearLayout layout, it is necessary to set landscape or portrait! Otherwise you will not see the effect. Linear.setorientation (linearlayout.vertical); Linearlayout.layoutparams LP = new Linearlayout.layoutparams (layoutparams.wrap_content,layoutparams.wrap_content) ;//Here is equivalent to the android:layout_gravity attribute in the layout file Lp.gravity = Gravity.right;linear.addview (button, LP); Setcontentview (linear );

Well, it's not going to be the same as above. That's all I'm talking about.

Also, to set the location in Relativelayout, use the Addrule method, such as the following:

params = new Relativelayout.layoutparams (layoutparams.wrap_content, layoutparams.wrap_content);        Params.addrule (relativelayout.center_in_parent);        Mcontainer.addview (Progress,params);

Suppose that this article is helpful to you, also please leave a message to support, thank you!

"Android Layout" set Android:gravity and Android:layout_gravity properties in your program

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.