相对布局RelativeLayout常用属性

1.图片有多大布局的申请就有多大

         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
<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"
    tools:context=".MainActivity" >

    <RelativeLayout  
         android:id="@+id/abc1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/bg1"
        ></RelativeLayout>
    
    
</RelativeLayout>

运行效果

 

2.与父控件的左顶部对齐

android:layout_alignParentTop="true"
<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"
    tools:context=".MainActivity" >

    <RelativeLayout  
         android:id="@+id/abc1"
         android:layout_alignParentTop="true"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/bg1"
        ></RelativeLayout>
    
    
</RelativeLayout>

运行效果和上面一致

3.与父控件的左底部对齐

android:layout_alignParentBottom="true"
<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"
    tools:context=".MainActivity" >

    <RelativeLayout  
         android:id="@+id/abc1"
         android:layout_alignParentBottom="true"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/bg1"
        ></RelativeLayout>
    
    
</RelativeLayout>

运行效果

 

4.与父控件的右底部对齐

 android:layout_alignParentBottom="true"
 android:layout_alignParentRight="true"
<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"
    tools:context=".MainActivity" >

    <RelativeLayout  
         android:id="@+id/abc1"
         android:layout_alignParentBottom="true"
         android:layout_alignParentRight="true"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/bg1"
        ></RelativeLayout>
    
    
</RelativeLayout>

 运行效果

 

5.与父控件的右顶部对齐

android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
<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"
    tools:context=".MainActivity" >

    <RelativeLayout  
         android:id="@+id/abc1"
         android:layout_alignParentTop="true"
         android:layout_alignParentRight="true"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/bg1"
        ></RelativeLayout>
    
    
</RelativeLayout>

  运行效果

 

6.控件的右部和父控件的右部对齐

<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"
    tools:context=".MainActivity" >

    <RelativeLayout  
         android:id="@+id/abc1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/bg1"
        ></RelativeLayout>
    
     <RelativeLayout  
         android:layout_toRightOf="@id/abc1"
         android:layout_width="wrap_content"
         android:layout_height="400dp"
         android:background="@drawable/bg2"
        ></RelativeLayout>
     
</RelativeLayout>

 运行效果

7.控件的底部边缘与给定ID的底部边缘对齐

<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"
    tools:context=".MainActivity" >

    
    
     <RelativeLayout  
         android:id="@+id/abc2"
         android:layout_width="wrap_content"
         android:layout_height="400dp"
         android:background="@drawable/bg2"
        ></RelativeLayout>
     
     <RelativeLayout  
         android:id="@+id/abc1"
         android:layout_toRightOf="@+id/abc2"
          android:layout_alignBottom="@+id/abc2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/bg1"
        ></RelativeLayout>
</RelativeLayout>

 运行效果

8.水平居中:android:layout_centerHorizontal

<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"
    tools:context=".MainActivity" >

    
    
     <RelativeLayout  
         android:id="@+id/abc2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/bg2"
         android:layout_centerHorizontal="true"
        ></RelativeLayout>
     
     
</RelativeLayout>

运行效果:

9.垂直居中 android:layout_centerVertical

 

<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"
    tools:context=".MainActivity" >

    
    
     <RelativeLayout  
         android:id="@+id/abc2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/bg2"
         android:layout_centerVertical="true"
        ></RelativeLayout>
     
     
</RelativeLayout>

 运行效果:

10.父控件的中央: android:layout_centerInParent="true"

<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"
    tools:context=".MainActivity" >

    
    
     <RelativeLayout  
         android:id="@+id/abc2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/bg2"
         android:layout_centerInParent="true"
        ></RelativeLayout>
     
     
</RelativeLayout>

 运行效果

 注:输入 android:可以看到各种样式

       给新控件添加id的时候:@+id

       要使用控件的时候:          @id       

——@上官可编程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
相对布局RelativeLayout)是 Android 中常用的一种布局方式,可以根据控件之间的相对位置来确定它们的布局关系。以下是使用相对布局的步骤: 1. 在 XML 布局文件中,使用 `<RelativeLayout>` 标签将布局内容包裹起来。 2. 在 `<RelativeLayout>` 标签内部,使用 `<View>` 或其他布局控件标签来定义需要布局的控件。 3. 对于每个需要布局的控件,可以使用一些属性指定它们与其他控件的相对位置。常用属性包括: - `android:layout_alignParentTop`、`android:layout_alignParentBottom`、`android:layout_alignParentLeft`、 `android:layout_alignParentRight`:将控件与父容器的顶部、底部、左侧或右侧对齐。 - `android:layout_alignTop`、`android:layout_alignBottom`、`android:layout_alignLeft`、`android:layout_alignRight`: 将控件与指定控件的顶部、底部、左侧或右侧对齐。 - `android:layout_below`、`android:layout_above`、`android:layout_toLeftOf`、`android:layout_toRightOf`: 将控件放置在指定控件的下方、上方、左侧或右侧。 - `android:layout_centerInParent`:将控件居中于父容器。 - `android:layout_centerHorizontal`、`android:layout_centerVertical`:将控件水平或垂直居中于父容器。 这些属性可以与其他布局属性(如 `android:layout_width`、`android:layout_height`)结合使用,以确定控件的大小和位置。 4. 使用这些属性为每个控件设置相应的相对位置关系,调整它们的顺序和布局方式,以满足设计需求。 下面是一个示例: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" android:layout_below="@id/button1" android:layout_alignParentRight="true"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" android:layout_toLeftOf="@id/button2" android:layout_below="@id/button1" android:layout_marginRight="10dp"/> </RelativeLayout> ``` 在这个示例中,有三个按钮控件,它们的位置相互关联。第一个按钮位于父容器的顶部和左侧,第二个按钮位于第一个按钮的下方和父容器的右侧,第三个按钮位于第一个按钮和第二个按钮之间,并稍微向右偏移了一些距离。 这样,通过相对布局,我们可以根据控件之间的相对位置来灵活地排列和定位界面元素。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值