Android: layout_gravity and Android: gravity

Source: Internet
Author: User

Example 1: Android: gravity = "right": Align the content of a single view to the right.

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  <  Linearlayout  Xmlns: Android  = "Http://schemas.android.com/apk/res/android"  Android: layout_width  = "Fill_parent"  Android: layout_height  = "Fill_parent"  Android: gravity  = "Right"  >      <  Button  Android: ID  = "@ + ID/button1"  Android: layout_width  = "Wrap_content"  Android: layout_height  = "Wrap_content"  Android: Text  = "Button"   />  </  Linearlayout  > 

The button is on the right of linearlayout.

 

Example 2: Android: layout_gravity = "right" is right-aligned to the right of the view in the container.

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  <  Framelayout  Xmlns: Android  = "Http://schemas.android.com/apk/res/android"  Android: layout_width  = "Fill_parent"  Android: layout_height  = "Fill_parent"  Android: Orientation  = "Horizontal"  >      <  Textview  Android: ID  = "@ + ID/textview1"  Android: layout_width  = "Wrap_content"  Android: layout_height  = "Wrap_content"  Android: layout_gravity  = "Left"  Android: Text  = "Textview"   />      < Textview  Android: ID  = "@ + ID/textview2"  Android: layout_width  = "Wrap_content"  Android: layout_height  = "Wrap_content"  Android: layout_gravity  = "Right"  Android: Text  = "Textview"   />  </  Framelayout  > 

The display result is textview1 to the left and textview2 to the right.

Note:: Framelayout is used here. If linearlayout is used,

When Android: Orientation = "horizontal", the horizontal direction is left to right.

When Android: Orientation = "vertical", it can only be horizontally aligned, and the vertical direction is from top to bottom.

 

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.