In a linear layout, like the name suggests, all the elements are displayed in a linear fashion(below is an example of the linear layouts), either Horizontally or Vertically and this behavior is set inandroid:orientation which is an attribute of the node LinearLayout.
Example of Vertical layout snippet
<
LinearLayout
android:orientation
=
"vertical"
> .... </
LinearLayout
>
&
<
LinearLayout
android:orientation
=
"horizontal"
> .... </
LinearLayout
>
Now that we know the two types of linear layouts, here are the steps you need to follow to create them
1. Create a new project File -> New -> Android Project
2. In Package Explorer right click on res/layout folder and create a new Android XML File and name it as you wish. I am naming it as “linear_layout.xml”
res/layout -> Right Click -> New -> Android XML File
3. Now open newly created xml file (in my case “linear_layout.xml”) and type the following code.
2. In Package Explorer right click on res/layout folder and create a new Android XML File and name it as you wish. I am naming it as “linear_layout.xml”
res/layout -> Right Click -> New -> Android XML File
3. Now open newly created xml file (in my case “linear_layout.xml”) and type the following code.
<? xml version = "1.0" encoding = "utf-8" ?> <!-- Parent linear layout with vertical orientation --> < LinearLayout android:orientation = "vertical" android:layout_width = "match_parent" android:layout_height = "match_parent" > < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "Email:" android:padding = "5dip" /> < EditText android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:layout_marginBottom = "10dip" /> < Button android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "Login" /> <!-- Child linear layout with horizontal orientation --> < LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:orientation = "horizontal" android:background = "#2a2a2a" android:layout_marginTop = "25dip" > < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "Home" android:padding = "15dip" android:layout_weight = "1" android:gravity = "center" /> < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "About" android:padding = "15dip" android:layout_weight = "1" android:gravity = "center" /> </ LinearLayout > </ LinearLayout >
To set this newly created view as the initial view of your app, Open your MainActivity.java file. You would see the following line inside the onCreate function setContentView(R.layout.main). Change R.layout.main to R.layout.yourlinearviewname. In my case itsR.layout.linear_layout
package com.example.androidlayouts; import android.app.Activity; import android.os.Bundle; public class AndroidLayoutsActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.linear_layout); } }
To run the application, right click on the project -> Run As -> 1. Android Application. You should see your newly created linear layout in the emulator.
|
No comments:
Post a Comment