Android Layout
Android Layout
LAYOUT
L. Grewe
2.
The above will create an interface in vertical (versus portrait) mode that fills the parent
Both in width and write and wraps and content as necessary.
XML interface
Build up UI quickly
XML interface
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"/>
xmlns:android XML namespace declaration that tells the Android tools that
you are going to refer to common attributes defined in the Android
namespace. The outermost tag in every Android layout file must have this
attribute.
android:text This sets the text that the TextView should display. In this
example, you use a string resource instead of a hard-coded string value.
The hello string is defined in the res/values/strings.xml file.
UsingEclipseIDEtoVisuallyCreate
XMLfile
UsingEclipseIDEtoVisuallyCreate
XMLfile
Layout Tags
Control structure of interface
Layout Tags
LinearLayout
A Layout that arranges its children in a single column or a single row. The
direction of the row can be set by calling setOrientation(). You can also
specify gravity, which specifies the alignment of all the child elements by
calling setGravity() or specify that specific children grow to fill up any
remaining space in the layout by setting the weight member of
LinearLayout.LayoutParams. The default orientation is horizontal.
AbsoluteLayout
A layout that lets you specify exact locations (x/y coordinates) of its
children. Absolute layouts are less flexible and harder to maintain than
other types of layouts without absolute positioning.
RelativeLayout
FrameLayout
TableLayout
LinearLayoutXMLtag
Attributes
android:spacing setSpacing(int)
android:unselectedAlpha setUnselectedAlpha(float) Sets the
alpha on the items that are not selected.
Codesetting up Gallery
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int
position, long id) {
Toast.makeText(HelloGallery.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
android.widget.LinearLayout
android.widget.AbsoluteLayout
android.widget.TableLayout
android.widget.RelativeLayout
android.widget.FrameLayout
android.widget.ScrollLayout
LinearLayout
(<LinearLayout> or
arranges by single column or row.
android.widget.LinearLayou
child views can be arranged vertically or
horizontally. <?xml version="1.0"
t)
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Text View
android:layout_width=fill_parent
android:layout_height=wrap_content
android:text=@string/hello/>
</LinearLayout>
LinearLayout attributes
i.e. Xml
android:orientation=vertical
code (ll is LinearLayout instance)
ll.setOrientation(VERTICAL);
Description
layout_width
layout_height
specifies height
layout_marginTop
layout_marginBottom
layout_marginLeft
layout_marginRight
layout_gravity
layout_weight
layout_x
x-coordinate