【Android Studio程序开发】常用布局--相对布局RelativeLayout

17 篇文章 2 订阅
12 篇文章 2 订阅

线性布局的下级视图是顺序排列着的,另一种相对布局的下级视图位置则由其他视图决定。相对布局名 为RelativeLayout,因为下级视图的位置是相对位置,所以得有具体的参照物才能确定最终位置。如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角。 用于确定下级视图位置的参照物分两种,一种是与该视图自身平级的视图;另一种是该视图的上级视图 (也就是它归属的RelativeLayout)。综合两种参照物,相对位置在XML文件中的属性名称说明见下表。

 为了更好地理解上述相对属性的含义,接下来使用RelativeLayout及其下级视图进行布局来看看实际效果图。下面是演示相对布局的XML文件例子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="150dp" >

        <TextView

        android:id="@+id/tv_center"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:background="#ffffff"

        android:text="我在中间"

        android:textSize="11sp"

        android:textColor="#000000" />

    
    <TextView

        android:id="@+id/tv_center_horizontal"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:background="#eeeeee"

        android:text="我在水平中间"

        android:textSize="11sp"

        android:textColor="#000000" />

    
    <TextView

        android:id="@+id/tv_center_vertical"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerVertical="true"

        android:background="#eeeeee"

        android:text="我在垂直中间"

        android:textSize="11sp"

        android:textColor="#000000" />

    
    <TextView

        android:id="@+id/tv_parent_left"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:background="#eeeeee"

        android:text="我跟上级左边对齐"

        android:textSize="11sp"

        android:textColor="#000000" />

    <TextView

        android:id="@+id/tv_parent_right"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentRight="true"

        android:background="#eeeeee"

        android:text="我跟上级右边对齐"

        android:textSize="11sp"

        android:textColor="#000000" />

    <TextView

        android:id="@+id/tv_parent_top"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"

        android:background="#eeeeee"

        android:text="我跟上级顶部对齐"

        android:textSize="11sp"
        android:textColor="#000000" />

    <TextView

        android:id="@+id/tv_parent_bottom"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:background="#eeeeee"

        android:text="我跟上级底部对齐"

        android:textSize="11sp"

        android:textColor="#000000" />

    
    <TextView

        android:id="@+id/tv_left_center"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_toLeftOf="@+id/tv_center"

        android:layout_alignTop="@+id/tv_center"

        android:background="#eeeeee"

        android:text="我在中间左边"

        android:textSize="11sp"

        android:textColor="#000000" />

    
    <TextView

        android:id="@+id/tv_right_center"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_toRightOf="@+id/tv_center"

        android:layout_alignBottom="@+id/tv_center"

        android:background="#eeeeee"

        android:text="我在中间右边"

        android:textSize="11sp"

        android:textColor="#000000" />

    
    <TextView

        android:id="@+id/tv_above_center"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_above="@+id/tv_center"

        android:layout_alignLeft="@+id/tv_center"

        android:background="#eeeeee"

        android:text="我在中间上面"

        android:textSize="11sp"

        android:textColor="#000000" />

    
    <TextView

        android:id="@+id/tv_below_center"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/tv_center"

        android:layout_alignRight="@+id/tv_center"

        android:background="#eeeeee"

        android:text="我在中间下面"

        android:textSize="11sp"

        android:textColor="#000000" />
</RelativeLayout>

上述XML文件的布局效果如下图所示,RelativeLayout的下级视图都是文本视图,控件上的文字说明 了所处的相对位置,具体的控件显示方位正如XML属性中描述的那样。

Java程序如下:

package com.example.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class RelativeLayoutActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relative_layout);
    }
}

运行前一定要注意清单文件 

感谢观看!!!

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值