Beruflich Dokumente
Kultur Dokumente
Android offers a collection of view classes that act as containers for views. These
container classes are called layouts (or layout managers), and each implements a
specific strategy to manage the size and position of its children.
• For example, the LinearLayout class lays out its children either horizontally or
vertically, one after the other.
• All layout managers derive from the View class, therefore you can nest layout
managers inside of one another.
Layout Managers:
LinearLayout :Organizes its children either
horizontally or vertically
TableLayout :Organizes its children in tabular form
RelativeLayout :Organizes its children relative to
one another or to the parent
FrameLayout :Allows you to dynamically change
the control(s) in the layout
GridLayout :Organizes its children in a grid
arrangement
The LinearLayout Layout Manager
The LinearLayout layout manager is the most basic. This layout
manager organizes its children either horizontally or vertically based
on the value of the orientation property.
LinearLayout with a Horizontal Configuration
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_
<!-- add children here-->
</LinearLayout>
For example, you can set android:gravity to center to have the text in
the EditText centered within the control. Similarly, you can align an
EditText to the far right of a LinearLayout (the container) by setting
android:layout_gravity=“right”
Understanding the Difference Between android:gravity
and android:layout_gravity
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content" android:text="one"
android:layout_gravity="right"/>
</LinearLayout>
The TableLayout Layout Manager
Populating FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frmLayout"
android:layout_width="fill_parent" android:layout_height="fill_<ImageView
android:id="@+id/oneImgView"
android:src="@drawable/one"
android:scaleType="fitCenter"
android:layout_width="fill_parent" android:layout_height="<ImageView
android:id="@+id/twoImgView"
android:src="@drawable/two"
android:scaleType="fitCenter"
android:layout_width="fill_parent"
android:layout_height="android:visibility="gone" />
</FrameLayout>
public class FrameLayoutActivity extends Activity{
private ImageView one = null;
private ImageView two = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listing6_48);
one = (ImageView)findViewById(R.id.oneImgView);
two = (ImageView)findViewById(R.id.twoImgView);
one.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
two.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
}});
two.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
one.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
}});
}
}
shows the layout file as well as the onCreate() method of the activity.
The idea of the demonstration is to load two ImageView objects in the FrameLayout,
with only one of the ImageView objects visible at a time. In the UI, when the user clicks
the visible image, we hide one image and show the other one.
Working with Menus and Action Bars
Android SDK supports regular menus, submenus, context menus, icon menus, and
secondary menus.
Android 3.0 introduced the action bar, which integrates well with menus