How to create Linear Layout:
In a standard screen user interface (UI) layout, buttons are
usually placed across the top of the
screen, or sometimes down the side of the screen, in a
straight line. This is exactly what the
LinearLayout class does for you. It is designed to contain
and automatically arrange UI elements
placed inside of it across the screen (using the horizontal
orientation parameter) or up and down the
screen (using the vertical orientation parameter).
To set the LinearLayout object’s orientation, use the .setOrientation(integer)
method, with either the constant HORIZONTAL for horizontal
or VERTICAL for vertical:
myLinearLayout.setOrientation(HORIZONTAL);
Here’s the attribute for orientation in the LinearLayout tag
for XML:
android:orientation="vertical"
Thus, the entire LinearLayout opening tag for a Vertical UI
layout looks like this:
<LinearLayout
android:orientation="vertical">
so here’s how a Horizontal UI layout would normally be
coded:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout width="match parent"
android:layout height="match parent"
android:orientation="horizontal">
<The Items To Be Arranged Horizontally Would Be Inside
This Container>
</LinearLayout>
The first parameter of the LinearLayout XML tag is the path
to the Android XML naming schema
definition (xmlns:android stands for: eXtensible Markup
Language Naming Schema for Android).
The value for the layout width and height parameters, match
parent, simply tells the LinearLayout
to expand to match its parent container size. Because this
is the top level LinearLayout container,
the android:layout_width=“match_parent” would mean to fill
the Android device (smartphone, tablet,
e-reader, or iTV) display screen width from one side to the
other side. We already know what the
orientation does, so now we have our LinearLayout defined.
Anything we place inside this container
will display horizontally across the screen, from left to
right.
Create a android project and add following piece of code in
activity_main.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout width="match parent"
android:layout height="match parent" >
<TextView
android:layout width="wrap content"
android:layout height="wrap content"
android:text="@string/textareaone"
tools:context=".MainActivity" />
<TextView
android:layout width="wrap content"
android:layout height="wrap content"
android:text="@string/textareatwo"
tools:context=".MainActivity" />
</LinearLayout>
Go to string.xml,
<resources>
<string name="app name">LinearLayout
Example</string>
<string name="textareaone">Text Area
One!</string>
<string name="textareatwo">Text Area
Two!</string>
<string name="menu
settings">Settings</string>
<string name="title activity
main">MainActivity</string>
</resources>
MainActivity.java :
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(savedInstanceState);
setContentView(R.menu.activity main, menu);
return true;
}
}
Run the Application
No comments:
Post a Comment