绝对布局,例子:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="红色"
android:gravity="center_horizontal|center_vertical"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="绿色"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="蓝色"
android:gravity="center|bottom"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="黄色"
android:gravity="bottom"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="第一行"
android:textSize="15sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="第二行"
android:textSize="15sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="第三行"
android:textSize="15sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="第四列"
android:textSize="15sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
相对布局,例:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#0000FF"
android:padding="10px"
>
<TextView
android:id="@+id/tv01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:text="Type here:"
/>
<EditText
android:id="@+id/txt01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv01"
/>
<Button
android:id="@+id/btn01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/txt01"
android:layout_alignParentRight="true"
android:text="OK"
/>
<Button
android:id="@+id/btn02"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/txt01"
android:layout_toLeftOf="@id/btn01"
android:layout_marginRight="30dp"
android:text="Cancel"
/>
</RelativeLayout>
绝对,相对混用,例子:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/id_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="id:"
>
</TextView>
<EditText
android:id="@+id/id_editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
>
</EditText>
<TextView
android:id="@+id/name_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name:"
>
</TextView>
<EditText
android:id="@+id/name_editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
>
</EditText>
<TextView
android:id="@+id/sno_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sno:"
>
</TextView>
<EditText
android:id="@+id/sno_editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
>
</EditText>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/createButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:text="创建"
>
</Button>
<Button
android:id="@+id/insertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/createButton"
android:text="插入"
>
</Button>
<Button
android:id="@+id/queryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/createButton"
android:text="查询全部"
>
</Button>
<Button
android:id="@+id/queryButtonSomeOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/queryButton"
android:text="单查询"
>
</Button>
<Button
android:layout_width="wrap_content"
android:text="更新"
android:layout_height="wrap_content"
android:id="@+id/updateButton"
android:layout_toRightOf="@+id/queryButtonSomeOne"
>
</Button>
<Button
android:layout_width="wrap_content"
android:text="删除"
android:layout_height="wrap_content"
android:id="@+id/deleteButton"
android:layout_toRightOf="@+id/updateButton"
>
</Button>
</RelativeLayout>
<TextView
android:id="@+id/displayResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="display result..."
android:layout_gravity="center_horizontal"
>
</TextView>
一些常用参数:
RelativeLayout用到的一些重要的属性:
EditText的android:hint
设置EditText为空时输入框内的提示信息。
android:gravity
android:gravity属性是对该view 内容的限定.比如一个button上面的text.
android:layout_gravity
android:layout_gravity是用来 设置该view相对与起父view 的位置.比如一个button在linearlayout里,你想把该button放在靠左、靠右等位置就可以通过该属性设置.以button为例,android:layout_gravity="right"则button靠右
android:layout_alignParentRight
使当前控件的右端和父控件的右端对齐。这里属性值只能为true或false,默认false。
android:scaleType:
android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType/ android:scaleType值的意义区别:
CENTER /center
CENTER_CROP / centerCrop
CENTER_INSIDE / centerInside
FIT_CENTER / fitCenter
FIT_END / fitEnd
FIT_START / fitStart
FIT_XY / fitXY
MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。
** 要注意一点,Drawable文件夹里面的图片命名是不能大写的。