Work with Relative Layout :
Relative layouts are for the more complicated UI layouts
where you need to define the UI elements in not quite a linear fashion. In
fact, the RelativeLayout tag is the default tag used in the standard blank activity
code bootstrap provided by the New ➤ Project ➤ Android Application project sequence of dialogs.
The RelativeLayout class allows you to define how the UI
elements (the View objects) are to be
placed on the screen relative to each other, rather than
just laid out linearly. For this reason, the
XML definition for a RelativeLayout may contain more screen
layout variables and parameters, so
this example will be a number of lines of markup code longer
(eight additional lines of code, or 50%
more code, to be exact) than the LinearLayout example.
If you start to get into the habit of nesting several LinearLayout
containers to achieve a more
complex UI layout result, you may want to consider using a
single RelativeLayout container to
achieve the same results with better control, fewer nested
levels, and more efficient XML code.
Simpler is always better, so if you can write a UI layout
using fewer nested ViewGroup containers,
it will always use less memory, and will function more
quickly (smoothly). The RelativeLayout
container allows you to arrange all sorts of UI elements
together in a single ViewGroup to achieve a
more complex layout using only a single layout container.
RelativeLayouts are also the optimal type of layout
container for using sliding drawers, another direct subclass of the ViewGroup class.
Sliding drawers extend the screen real estate of the smartphone, tablet, or iTV
by allowing drawers to slide out onto the screen from any of the four sides
(top, left, bottom, or right). This is very cool layout container functionality
(class) that is built into the Android SDK.
Because our Java code references activity_main.xml, we do
not need to change anything in the
MainActivity.java tab to make these changes work, a
testimony to the power of modularity via XML
in Android. We also do not need to change (or even remove)
the content in strings.xml, even though
the second text area string setting will not be used in the
application anymore.
In the first tag of activity_main.xml, change LinearLayout and
its closing tag back to RelativeLayout
and remove the android:orientation="vertical"
parameter. We will add some UI elements to the
inside of the RelativeLayout tag.
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout width="match parent"
android:layout height="match parent" >
<TextView
android:id="@+id/label"
android:layout width="wrap content"
android:layout height="wrap content"
android:text="Type here:" (Generates Warning: Hard
Coded Text)
tools:context=".MainActivity" />
<EditText (Generates Warning: No Hint Parameter Included)
android:id="@+id/editText1"
android:layout width="match parent"
android:layout height="wrap content"
android:layout below="@+id/label"
android:layout marginTop="10dip"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout width="wrap content"
android:layout height="wrap content"
android:layout alignParentRight="true"
android:layout below="@+id/editText1"
android:text="OK"/> (Generates Warning: hard
Coded Text)
<Button
android:id="@+id/button2"
android:layout width="wrap content"
android:layout height="wrap content"
android:layout below="@+id/editText1"
android:text="Cancel"/> (Generates Warning:
Hard Coded Text)
</RelativeLayout>
No comments:
Post a Comment