Common attributes
Description
Each View
and ViewGroup
has a set of common attributes.
- layout_width specifies the width of the View or ViewGroup
- layout_height specifies the height of the View or ViewGroup
- layout_marginTop specifies extra space on the top side of the View or ViewGroup
- layout_marginBottom specifies extra space on the bottom side of the View or ViewGroup
- layout_marginLeft specifies extra space on the left side of the View or ViewGroup
- layout_marginRight specifies extra space on the right side of the View or ViewGroup
- layout_gravity specifies how child Views are positioned
- layout_weight specifies how much of the extra space in the layout should be allocated to the View
- layout_x specifies the x-coordinate of the View or ViewGroup
- layout_y specifies the y-coordinate of the View or ViewGroup
Note
The layout_weight
and layout_gravity
attributes
are applicable only when a View is in either
a LinearLayout
or a TableLayout
.
<?xml version="1.0" 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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
For example, the width of the <TextView>
element fills the
entire width of its parent using the fill_parent
constant.
Its height is indicated by the wrap_content
constant, which means that its height is the height of its
content (in this case, the text contained within it).
If you don't want the <TextView>
view to occupy the entire row, you can set its
layout_width
attribute to wrap_content
, like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />
The preceding code will set the width of the view to be equal to the width of the text contained within it.