从零开始学安卓笔记:相对布局

149 阅读2分钟

相对布局概述:

- 最灵活的布局
- 描述容器与控件的位置关系
- 描述控件与控件的位置关系

相对布局的常用属性:

元素与元素的相对位置:
属性名称说明
android:layout_above设置本控件在某控件的上方
android:layout_below设置本控件在某控件的下方
android:layout_toLeftOf设置本控件在某控件的左方
android:layout_toRightOf设置本控件在某控件的右方
元素与容器的相对位置:
属性名称说明
android:layout_alignParentTop设置本控件在容器的顶部
android:layout_alignParentBottom设置本控件在容器的底部
android:layout_alignParentLeft设置本控件在容器的左边
android:layout_alignParentRight设置本控件在容器的右边
元素在容器中的居中关系:
属性名称说明
android:layout_centerHorizontal设置本控件在容器中水平居中
android:layout_centerVertical设置本控件在容器中垂直居中
android:layout_centerInParent设置本控件在容器中双向居中

示例:

<?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">

    <!--    头部-->
    <RelativeLayout
            android:id="@+id/top"
            android:background="#00f"
            android:layout_width="match_parent"
            android:layout_height="100dp">

    </RelativeLayout>

    <!--    底部-->
    <RelativeLayout
            android:id="@+id/bottom"
            android:background="#0ff"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="80dp">

    </RelativeLayout>

    <!--    中间部分-->
    <RelativeLayout
            android:id="@+id/center"
            android:background="#ff0"
            android:layout_below="@+id/top"
            android:layout_above="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

    </RelativeLayout>

</RelativeLayout>

全屏显示:

如何去掉状态栏和标题栏?
代码实现:
requestWindowFeature(Window.FEATURE_NO_TITLE);// 去标题栏

getWindow().setFlags(
        WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN
);// 全屏

getSupportActionBar().hide();
应用场景:
    --普通应用:无标题,但保留状态栏
    --游戏应用:全屏显示

相对布局的应用:

【特别注意】
    --layout_above属性必须设置,否则将会被底部遮挡
    --先出现的id要添加+号
    --子布局在XML中的顺序不重要(可以先声明底部容器)

示例:

btn_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
        android:shape="rectangle"
        xmlns:android="http://schemas.android.com/apk/res/android">

<!--    圆角的半径-->
    <corners android:radius="10dp"/>
<!--    填充颜色-->
    <solid android:color="@color/colorPrimary"/>
</shape>
btn_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
        android:shape="rectangle"
        xmlns:android="http://schemas.android.com/apk/res/android">

    <!--    圆角的半径-->
    <corners android:radius="10dp"/>
    <!--    填充颜色-->
    <solid android:color="@color/colorAccent"/>
</shape>
btn_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--    正常状态-->
    <item android:drawable="@drawable/btn_normal" android:state_pressed="false"/>
<!--    按下状态-->
    <item android:drawable="@drawable/btn_pressed" android:state_pressed="true"/>
</selector>
<Button
        android:id="@+id/btn"
        android:text="卸载"
        android:textColor="#fff"
        android:textSize="14sp"
        android:background="@drawable/btn_selector"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_width="66dp"
        android:layout_height="30dp"/>

总结:

--相对布局中对子控件和子布局一般都需要添加id
--相对布局可以允许子控件和子布局在xml中不按顺序声明
--子布局之间需要声明相对位置才可避免遮盖
--在Draw-9-patch工具中通过左上两条黑线来标记可拉伸部分
--在Draw-9-patch工具中通过右下两条黑线来标记显示文字部分

--  _erizo