Android - UI Layouts

<p >The basic building block for user interface is a<b >View</b>object which is created from the View class and occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components like buttons, text fields, etc.</p> <p >The<b >ViewGroup</b>is a subclass of<b >View</b>and provides invisible container that hold other Views or other ViewGroups and define their layout properties.</p> <p >At third level we have different layouts which are subclasses of ViewGroup class and a typical layout defines the visual structure for an Android user interface and can be created either at run time using<b >View/ViewGroup</b>objects or you can declare your layout using simple XML file<b >main_layout.xml</b>which is located in the res/layout folder of your project.</p> <p><img alt="Layout" src="http://www.tutorialspoint.com/android/images/layout.jpg" /></p> <h4 align="center" >LAYOUT PARAMS</h4> <p >This tutorial is more about creating your GUI based on layouts defined in XML file. A layout may contain any type of widgets such as buttons, labels, textboxes, and so on. Following is a simple example of XML file having LinearLayout:</p> <pre class="prettyprint notranslate" > &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; android:orientation=&quot;vertical&quot; &gt; &lt;TextView android:id=&quot;@+id/text&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;This is a TextView&quot; /&gt; &lt;Button android:id=&quot;@+id/button&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;This is a Button&quot; /&gt; &lt;!-- More GUI components go here --&gt; &lt;/LinearLayout&gt; </pre> <p >Once your layout has created, you can load the layout resource from your application code, in your<i >Activity.onCreate()</i>callback implementation as shown below &minus;</p> <pre class="prettyprint notranslate" > public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } </pre> <h2 >Android Layout Types</h2> <p >There are number of Layouts provided by Android which you will use in almost all the Android applications to provide different view, look and feel.</p> <table class="table table-bordered" > <tbody > <tr > <th >Sr.No</th> <th >Layout &amp; Description</th> </tr> <tr > <td >1</td> <td > <p ><a alt="Android Linear Layout" href="http://www.tutorialspoint.com/android/android_linear_layout.htm" >Linear Layout</a></p> <p >LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally.</p> </td> </tr> <tr > <td >2</td> <td > <p ><a alt="Android Relative Layout" href="http://www.tutorialspoint.com/android/android_relative_layout.htm" >Relative Layout</a></p> <p >RelativeLayout is a view group that displays child views in relative positions.</p> </td> </tr> <tr > <td >3</td> <td > <p ><a alt="Android Table Layout" href="http://www.tutorialspoint.com/android/android_table_layout.htm" >Table Layout</a></p> <p >TableLayout is a view that groups views into rows and columns.</p> </td> </tr> <tr > <td >4</td> <td > <p ><a alt="Android Absolute Layout" href="http://www.tutorialspoint.com/android/android_absolute_layout.htm" >Absolute Layout</a></p> <p >AbsoluteLayout enables you to specify the exact location of its children.</p> </td> </tr> <tr > <td >5</td> <td > <p ><a alt="Android Frame Layout" href="http://www.tutorialspoint.com/android/android_frame_layout.htm" >Frame Layout</a></p> <p >The FrameLayout is a placeholder on screen that you can use to display a single view.</p> </td> </tr> <tr > <td >6</td> <td > <p ><a alt="Android List View" href="http://www.tutorialspoint.com/android/android_list_view.htm" >List View</a></p> <p >ListView is a view group that displays a list of scrollable items.</p> </td> </tr> <tr > <td >7</td> <td > <p ><a alt="Android Linear Layout" href="http://www.tutorialspoint.com/android/android_grid_view.htm" >Grid View</a></p> <p >GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.</p> </td> </tr> </tbody> </table> <h2 >Layout Attributes</h2> <p >Each layout has a set of attributes which define the visual properties of that layout. There are few common attributes among all the layouts and their are other attributes which are specific to that layout. Following are common attributes and will be applied to all the layouts:</p> <table class="table table-bordered" > <tbody > <tr > <th >Attribute</th> <th >Description</th> </tr> <tr > <td >android:id</td> <td >This is the ID which uniquely identifies the view.</td> </tr> <tr > <td >android:layout_width</td> <td >This is the width of the layout.</td> </tr> <tr > <td >android:layout_height</td> <td >This is the height of the layout</td> </tr> <tr > <td >android:layout_marginTop</td> <td >This is the extra space on the top side of the layout.</td> </tr> <tr > <td >android:layout_marginBottom</td> <td >This is the extra space on the bottom side of the layout.</td> </tr> <tr > <td >android:layout_marginLeft</td> <td >This is the extra space on the left side of the layout.</td> </tr> <tr > <td >android:layout_marginRight</td> <td >This is the extra space on the right side of the layout.</td> </tr> <tr > <td >android:layout_gravity</td> <td >This specifies how child Views are positioned.</td> </tr> <tr > <td >android:layout_weight</td> <td >This specifies how much of the extra space in the layout should be allocated to the View.</td> </tr> <tr > <td >android:layout_x</td> <td >This specifies the x-coordinate of the layout.</td> </tr> <tr > <td >android:layout_y</td> <td >This specifies the y-coordinate of the layout.</td> </tr> <tr > <td >android:layout_width</td> <td >This is the width of the layout.</td> </tr> <tr > <td >android:layout_width</td> <td >This is the width of the layout.</td> </tr> <tr > <td >android:paddingLeft</td> <td >This is the left padding filled for the layout.</td> </tr> <tr > <td >android:paddingRight</td> <td >This is the right padding filled for the layout.</td> </tr> <tr > <td >android:paddingTop</td> <td >This is the top padding filled for the layout.</td> </tr> <tr > <td >android:paddingBottom</td> <td >This is the bottom padding filled for the layout.</td> </tr> </tbody> </table> <p >Here width and height are the dimension of the layout/view which can be specified in terms of dp (Density-independent Pixels), sp ( Scale-independent Pixels), pt ( Points which is 1/72 of an inch), px( Pixels), mm ( Millimeters) and finally in (inches).</p> <p >You can specify width and height with exact measurements but more often, you will use one of these constants to set the width or height &minus;</p> <ul class="list" > <li > <p ><b >android:layout_width=wrap_content</b>tells your view to size itself to the dimensions required by its content.</p> </li> <li > <p ><b >android:layout_width=fill_parent</b>tells your view to become as big as its parent view.</p> </li> </ul> <p >Gravity attribute plays important role in positioning the view object and it can take one or more (separated by &#39;|&#39;) of the following constant values.</p> <table class="table table-bordered" > <tbody > <tr > <th >Constant</th> <th >Value</th> <th >Description</th> </tr> <tr > <td >top</td> <td >0x30</td> <td >Push object to the top of its container, not changing its size.</td> </tr> <tr > <td >bottom</td> <td >0x50</td> <td >Push object to the bottom of its container, not changing its size.</td> </tr> <tr > <td >left</td> <td >0x03</td> <td >Push object to the left of its container, not changing its size.</td> </tr> <tr > <td >right</td> <td >0x05</td> <td >Push object to the right of its container, not changing its size.</td> </tr> <tr > <td >center_vertical</td> <td >0x10</td> <td >Place object in the vertical center of its container, not changing its size.</td> </tr> <tr > <td >fill_vertical</td> <td >0x70</td> <td >Grow the vertical size of the object if needed so it completely fills its container.</td> </tr> <tr > <td >center_horizontal</td> <td >0x01</td> <td >Place object in the horizontal center of its container, not changing its size.</td> </tr> <tr > <td >fill_horizontal</td> <td >0x07</td> <td >Grow the horizontal size of the object if needed so it completely fills its container.</td> </tr> <tr > <td >center</td> <td >0x11</td> <td >Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.</td> </tr> <tr > <td >fill</td> <td >0x77</td> <td >Grow the horizontal and vertical size of the object if needed so it completely fills its container.</td> </tr> <tr > <td >clip_vertical</td> <td >0x80</td> <td >Additional option that can be set to have the top and/or bottom edges of the child clipped to its container&#39;s bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.</td> </tr> <tr > <td >clip_horizontal</td> <td >0x08</td> <td >Additional option that can be set to have the left and/or right edges of the child clipped to its container&#39;s bounds. The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.</td> </tr> <tr > <td >start</td> <td >0x00800003</td> <td >Push object to the beginning of its container, not changing its size.</td> </tr> <tr > <td >end</td> <td >0x00800005</td> <td >Push object to the end of its container, not changing its size.</td> </tr> </tbody> </table> <h2 >View Identification</h2> <p >A view object may have a unique ID assigned to it which will identify the View uniquely within the tree. The syntax for an ID, inside an XML tag is &minus;</p> <pre class="result notranslate" > android:id=&quot;@+id/my_button&quot; </pre> <p >Following is a brief description of @ and + signs &minus;</p> <ul class="list" > <li > <p >The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource.</p> </li> <li > <p >The plus-symbol (+) means that this is a new resource name that must be created and added to our resources. To create an instance of the view object and capture it from the layout, use the following &minus;</p> </li> </ul> <pre class="prettyprint notranslate" > Button myButton = (Button) findViewById(R.id.my_button);</pre>
RangeTime:0.017663s
RangeMem:215.51 KB
返回顶部 留言