Android常用ViewGroup之RelativeLayou

2018-10-17  本文已影响9人  蜗牛猿猿

简介

相对布局可以让子控件相对于兄弟控件或父控件进行布局,可以设置子控件相对于兄弟控件或父控件进行上下左右对齐。
RelativeLayout能替换一些嵌套视图,当我们用LinearLayout来实现一个简单的布局但又使用了过多的嵌套时,就可以考虑使用RelativeLayout重新布局,来优化视图层级嵌套。

重要属性

相对布局中使用的属性分为相对父控件位置、子控件间相对位置、子控件在父控件中居中三大类。

android:layout_alignParentTop       取值布尔值,控件的顶部与父控件的顶部对齐
android:layout_alignParentBottom    取值布尔值,控件的底部与父控件的底部对齐
android:layout_alignParentLeft      取值布尔值,控件的左部与父控件的左部对齐
android:layout_alignParentRight     取值布尔值,控件的右部与父控件的右部对齐

android:layout_above                控件的底部置于给定ID的控件之上
android:layout_below                控件的底部置于给定ID的控件之下
android:layout_toLeftOf             控件的右边缘与给定ID的控件左边缘对齐
android:layout_toRightOf            控件的左边缘与给定ID的控件右边缘对齐
android:layout_alignBaseline        控件的baseline与给定ID的baseline对齐
android:layout_alignTop             控件的顶部边缘与给定ID的顶部边缘对齐
android:layout_alignBottom          控件的底部边缘与给定ID的底部边缘对齐
android:layout_alignLeft            控件的左边缘与给定ID的左边缘对齐
android:layout_alignRight           控件的右边缘与给定ID的右边缘对齐
android:layout_centerHorizontal     取值布尔值,水平居中
android:layout_centerVertical       取值布尔值,垂直居中
android:layout_centerInParent       取值布尔值,父控件的中央

示例

xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentTop="true"//对齐父控件顶部
        android:background="#aaaaaa"
        android:gravity="center"
        android:text="HelloWorld"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"//对齐父控件右边
        android:background="#aaaaaa"
        android:gravity="center"
        android:text="HelloWorld"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"//对齐父控件底部
        android:background="#aaaaaa"
        android:gravity="center"
        android:text="HelloWorld"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"//对齐父控件右边
        android:layout_alignParentBottom="true"//并且对齐父控件的底部
        android:background="#aaaaaa"
        android:gravity="center"
        android:text="HelloWorld"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_centerInParent="true"//父控件居中
        android:background="#aaaaaa"
        android:gravity="center"
        android:text="HelloWorld"
        android:textSize="20sp" />

</RelativeLayout>

这种效果相对布局很容易实现


效果2

xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#00FF78"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentTop="true"//对齐父控件顶部
        android:background="#aaaaaa"
        android:gravity="center"
        android:text="HelloWorld"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_hint"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"//对齐父控件右边
        android:background="#aaaaaa"
        android:gravity="center"
        android:text="HelloWorld"
        android:textSize="20sp" />


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_toLeftOf="@id/tv_hint"//在id为tv_hint的左边
        android:background="@mipmap/ic_launcher" />
</RelativeLayout>

注意

相对布局id的引用问题

示例xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#00FF78"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentTop="true"//对齐父控件顶部
        android:background="#aaaaaa"
        android:gravity="center"
        android:text="HelloWorld"
        android:textSize="20sp" />
        
   <ImageView
       android:layout_width="wrap_content"
       android:layout_height="40dp"
       android:layout_toLeftOf="@id/tv_hint"//注意此处引用id为tv_hint,
                                            但这个id在下面的TextView中创建
       android:background="@mipmap/ic_launcher" />
            
    <TextView
        android:id="@+id/tv_hint" //此处创建id为tv_hint
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:background="#aaaaaa"
        android:gravity="center"
        android:text="HelloWorld"
        android:textSize="20sp" />
</RelativeLayout>

上面的xml中ImageView希望在TextView的左边,但是ImageView写在了TextView的上面,引用了下面TextView的id,这个问题我最近遇到了,我的R文件无法生成一直报错,但是android studio没给提示,很头疼。这个问题在不同的环境上不一样,我在自己的电脑上运行没有任何问题,但是还是要注意,最好写在要引用id下面,防止被坑。

上一篇下一篇

猜你喜欢

热点阅读