android 相对布局

今天学习了RelativeLayout,线性布局不是一直能满足我们的要求,RelativeLayout允许子元素指定她们相对与其他元素或父元素的位置(通过id指定)。实例如下

-------------main.xml----------------

使用了RelativeLayout定义了布局方式

<?xml version="1.0" encoding="utf-8"?>

<!--
  android:background 定义了控件的背景色 
  android:layout_*:可以用来控制控件的位置
  
  android:layout_above:位于指定控件的上面
  android:layout_blow:位于指定控件的下面
  android:layout_toLeftOf:位于指定控件的左面
  android:layout_toRightOf:位于指定控件的右面
  
  android:layout_alignBottom:本控件的下边缘和指定控件下边缘对齐
  android:layout_alignTop:本控件的上边缘和指定控件的上边缘对齐
  android:layout_alignLeft:本控件的左边缘和指定控件的左边缘对齐
  android:layout_alignRight:本控件的右边缘和指定控件的右边缘对齐
  
  android:layout_alignParentButtom:如果值为true则指定控件和父控件的下边缘对齐
  android:layout_alignParentTop:如果值为true则指定控件和父控件的上边缘对齐
  android:layout_alignParentRight:如果值为true则指定控件和父控件的右边缘对齐
  android:layout_alignParentLeft:如果值为true则指定控件和父控件的左边缘对齐
  
  android:layout_centerHorizontal:如果值为true则指定控件再起父控件中水平居中
  android:layout_centerVertical:如果值为true则指定控件再起父控件中垂直居中
  android:layout_centerInParent:如果值为true则指定控件再起父控件中完全居中
  
 -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Type here:"/>
    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/label"/>
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/entry"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:text="确定" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/ok"
        android:layout_alignTop="@id/ok"
        android:text="取消" />
</RelativeLayout>
-------RelativeLayoutActivity---------
import android.app.Activity;
import android.os.Bundle;

public class RelativeLayoutActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值