[Android layout] sets the attributes of android: gravity and android: layout_Gravity in the program.

Source: Internet
Author: User

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: The gravity attribute limits the content in the view. For example, the text above a button can be set to the left or 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.

 

Let's go back to the subject,
You can set android: gravity = "center" to center the text in EditText in the EditText component.
Android: layout_gravity = "right" of EditText to display the EditText component right in LinearLayout. Check
Effect:

 

 

 

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:

 

[Xhtml]View plaincopyprint?
  1. <LinearLayout
  2. Xmlns: android = "http://schemas.android.com/apk/res/android"
  3. Android: orientation = "vertical"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent">
  6. <EditText
  7. Android: layout_width = "wrap_content"
  8. Android: gravity = "center"
  9. Android: layout_height = "wrap_content"
  10. Android: text = "one"
  11. Android: layout_gravity = "right"/>
  12. </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, you will find that if so, you can see the code.

 

 

 

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

 

[Java]View plaincopyprint?
  1. Button button = new Button (this );
  2. Button. setText ("One ");
  3. LinearLayout. LayoutParams lp = new LinearLayout. LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT );
  4. // This is equivalent to the Android: layout_gravity attribute in the layout file.
  5. Lp. gravity = Gravity. RIGHT;
  6. Button. setLayoutParams (lp );
  7. // This is equivalent to the Android: gravity attribute in the layout file.
  8. Button. setGravity (Gravity. CENTER );
  9. LinearLayout linear = new LinearLayout (this );
  10. // NOTE: For the LinearLayout layout, it is required to set the horizontal or vertical layout! Otherwise, you will not be able to see the effect.
  11. Linear. setOrientation (LinearLayout. VERTICAL );
  12. Linear. addView (button );
  13. SetContentView (linear );

 

 

Alternatively, you can:

 

[Java]View plaincopyprint?
  1. Button button = new Button (this );
  2. Button. setText ("One ");
  3. // This is equivalent to the Android: gravity attribute in the layout file.
  4. Button. setGravity (Gravity. CENTER );
  5. LinearLayout linear = new LinearLayout (this );
  6. // NOTE: For the LinearLayout layout, it is required to set the horizontal or vertical layout! Otherwise, you will not be able to see the effect.
  7. Linear. setOrientation (LinearLayout. VERTICAL );
  8. LinearLayout. LayoutParams lp = new LinearLayout. LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT );
  9. // This is equivalent to the Android: layout_gravity attribute in the layout file.
  10. Lp. gravity = Gravity. RIGHT;
  11. Linear. addView (button, lp );
  12. SetContentView (linear );

 

 

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

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.