Android学习笔记(九):Activity-RelativeLayout

指定widget在container的相对位置,包括:android:layout_alignParentTop, android:layout_alignParentBottom, android:layout_alignParentLeft, android:layout_alignParentRight, android:layout_centerHorizontal, android:layout_centerVertical, android:layout_centerInParent,他们的值是false|true。

如果是相对其他widget的位置,可以使用:android:layout_above, android:layout_below, android:layout_toLeftOf, android:layout_toRightOf。与其他widget位置对齐,可以使用:android:layout_alignTop, android:layout_alignBottom, android:layout_alignLeft, android:layout_alignRigh, android:layout_alignBaseline, 最后一个通常用于label的对齐。语法格式例子:layout_toRightOf="@id/widget_a"

我们将创建下面的Activity,Android XML如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- 我们采用RelativeLayout的布局,并设置了pad的留边,需要注意pad属于widget的有效范围 -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:padding="5px"
 >
<!-- textview是我们第一个基准widget,我们设置了android:paddingTop="15px"。-->
    <TextView android:id="@+id/label"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="URL:"
      android:paddingTop="15px" />
<!-- 在label的右面有一edittext,填满余下的空间,并和label进行对齐 -->
    <EditText android:id="@+id/entry"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@id/label"
      android:layout_alignBaseline="@id/label"

       />
<!-- 在edittext的下面并对齐最右方有一个OK button -->
    <Button android:id="@+id/ok"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/entry"
      android:layout_alignRight="@id/entry"

      android:text="OK"
       />
<!-- 在OK按键的左边增加一个Cancel button,并对齐。如果我们要求和OK的button之间增加间距,我们可以在下面增加设置android:laytou_marginRight="10px"
-->
    <Button android:id="@+id/cancel"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/ok"
      android:layout_alignTop="@id/ok"

      android:text="Cancel"
       />
</RelativeLayout>

textview是我们第一个基准widget,我们设置了android:paddingTop="15px",否则由于后面我们按此进行对齐,editiew会向上移并被chip最上端。由于editview上移,也导致了和下面button之间的间距过大。android是根据网格来安排widget的位置,每个widget都有一个确定的高度并匹配网格,如果widget被拉高,因为网格定位的缘故,button的相对位置并不会被抬高。同样的如果我们设置 android:paddingTop="30px",editview的位置下沉,同样由于网格的缘故,下面的button不会随着下沉,将和eidtiew的位置有所重叠,如图所示。

 

然则,我们怎么知道要给label设置15px,如果布局都需要根据这样的经验值,就相当郁闷,另一个解决方式,就是在定义edittext之前,就将label的对应位置根据其进行调整,然后再定义edittext。这在1.5版本之前有问题,因此我们需要设置AndroidManifest.xml,设定我们的最小运行版本环境,例如2.2,我们在manifest中设置:<uses-sdk android:minSdkVersion="8" /> 并且在default.properties文件中设置target=android-8,在Android的XML文件,处理如下:

    <TextView android:id="@+id/label"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="URL:"

       android:layout_alignBaseline="@+id/entry"
       android:layout_alignParentLeft="true"
       />

    <EditText android:id="@id/entry"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_toRightOf="@id/label"
       android:layout_alignParentTop="true"
        />

相关链接:我的Android开发相关文章

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: RelativeLayoutAndroid 开发中的一种布局方式,允许开发者通过相对位置和大小关系来安排组件的布局。RelativeLayout 可以使用相对位置和大小关系来定位和调整每个组件的位置,因此可以实现更灵活和复杂的布局。 ### 回答2: RelativeLayoutAndroid 中的一个布局管理器,它可以根据组件之间的相对关系进行布局。 在 RelativeLayout 中,我们可以使用多个属性来定义组件之间的相对关系,包括:上下左右的对齐、宽高的设定以及层级关系等。 相对位置的设定主要通过以下属性来实现: - layout_alignParentTop、layout_alignParentBottom、layout_alignParentLeft、layout_alignParentRight:表示组件与父容器的上下左右对齐关系。 - layout_alignTop、layout_alignBottom、layout_alignLeft、layout_alignRight:表示组件间的上下左右对齐关系。 - layout_below、layout_above、layout_toLeftOf、layout_toRightOf:表示组件的相对位置关系。 宽高的设定可以使用以下属性: - layout_width、layout_height:表示组件的宽高。 - layout_marginStart、layout_marginTop、layout_marginEnd、layout_marginBottom:表示组件的边距。 层级关系的设定可以使用以下属性: - layout_below:表示组件位于指定组件的下方。 - layout_above:表示组件位于指定组件的上方。 - layout_toLeftOf:表示组件位于指定组件的左侧。 - layout_toRightOf:表示组件位于指定组件的右侧。 通过使用这些属性,我们可以很方便地实现复杂的布局效果,但是相对布局也存在一些缺点,例如当布局关系复杂时,可能会导致层级嵌套过多,降低性能。 总的来说,RelativeLayout 是一个灵活且强大的布局管理器,可以满足大部分布局需求。但是在使用时需要注意保持布局简洁,避免嵌套过多,以免影响性能。 ### 回答3: RelativeLayoutAndroid中常用的布局类型之一。它以相对位置的方式定义了布局中各个视图之间的关系,使得开发者可以更加灵活地控制并排列视图。 相对布局可以让视图相对于父容器或其他视图进行定位。可以通过使用一些属性来指定视图在父容器或其他视图的位置关系,如alignParentTop、alignParentBottom、alignParentLeft、alignParentRight等。 此外,相对布局还提供了一组规则属性,通过设置这些规则属性,可以让视图相对于其他视图而不是父容器进行定位。比如通过layout_above、layout_below、layout_toLeftOf、layout_toRightOf等属性,来指定视图在其他视图的相对位置上的排列。 相对布局在嵌套布局中也很方便。通过使用android:layout_below、android:layout_alignLeft等属性,可以将一个视图放置在另一个视图的下方、左侧等位置。 相对布局还支持视图的填充方式。可以使用layout_centerVertical、layout_centerHorizontal等属性将视图在水平和垂直方向上居中显示。 总之,相对布局是一种灵活且强大的布局类型,通过指定视图之间的位置关系,可以实现各种复杂的视图排列效果。在Android应用开发中,相对布局是常用的布局之一,可以满足多样化的布局需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值