相对布局(下)

主要内容:

1 RelativeLayout布局的新属性(Android 4.2)

2 相对布局练习

相对布局的新属性:

1 android:layout_alignStart
2 android:layout_alignEnd
3 android:layout_alignParentStart
4 android:layout_alignParentEnd

试验:android:layout_alignEnd

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/firstView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:textSize="30sp"
        android:text="Hello" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00FF00"
        android:layout_below="@id/firstView"
        android:layout_alignEnd="@id/firstView"
        android:text="abc" />
    
</RelativeLayout>
效果:

可以看到android:layout_alignEnd的效果就是第二个控件的结束位置和第一个控件的结束位置是对齐的。其他类似

相对布局练习(使用相对布局画出下图所示的界面)


布局文件内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp" >

    <TextView
        android:id="@+id/labelView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="登录界面" />
    
    <EditText 
        android:id="@+id/username"
        android:layout_below="@id/labelView"
        android:layout_width="fill_parent"
        android:hint="username"
        android:layout_height="wrap_content"/>
    <EditText 
        android:id="@+id/password"
        android:layout_below="@id/username"
        android:hint="passwords"
        android:inputType="textPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/username"
        android:layout_alignRight="@+id/username"/>
    
    <Button 
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/password"
        android:layout_alignEnd="@+id/password"
        android:text="取消"        
        />
    <Button 
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/cancel"
        android:layout_toLeftOf="@id/cancel"
        android:text="确定"/>
    
</RelativeLayout>
注意: 登录界面的控件为EditText,默认显示的字符为hint属性,还可以设置密码的属性:inputType

效果如下:


作业:

使用相对布局实现:





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值