美文网首页
布局 - 相对布局

布局 - 相对布局

作者: C_G__ | 来源:发表于2019-04-25 14:15 被阅读0次

RelativeLayout 相对布局

相对布局也是一种非常常用的布局,它可以通过相对定位的方式让控件出现在任何位置。

属性

  • android:layout_alignParentLeft:同父容器左对齐。
  • android:layout_alignParentRight:同父容器右对齐。
  • android:layout_alignParentTop:同父容器顶对齐。
  • android:layout_alignParentBottom:同父容器底对齐。
  • android:layout_centerInParent:在父容器中居中。
    以上取值均为 true | false
  • android:layout_above:在某控件之上。
  • android:layout_below:在某控件之下。
  • android:layout_toLeftOf:在某控件左侧。
  • android:layout_toRightOf:在某控件右侧。
  • android:layout_alignLeft:同某控件左边缘对齐。
  • android:layout_alignRight:同某控件右边缘对齐。
  • android:layout_alignTop:同某控件顶边缘对齐。
  • android:layout_alignBottom:同某控件底边缘对齐。
    以上取值均为 @id/xxx

代码示例


activity_relative_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".chapter03.RelativeLayoutActivity"
    android:orientation="vertical">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#eeeeee"
                android:padding="3dp"
                android:text="相对于父容器布局"/>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp">

                <Button
                    android:id="@+id/btn_rela_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 1"
                    android:textAllCaps="false"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"/>

                <Button
                    android:id="@+id/btn_rela_2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 2"
                    android:textAllCaps="false"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"/>

                <Button
                    android:id="@+id/btn_rela_3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 3"
                    android:textAllCaps="false"
                    android:layout_centerInParent="true"/>

                <Button
                    android:id="@+id/btn_rela_4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 4"
                    android:textAllCaps="false"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentBottom="true"/>

                <Button
                    android:id="@+id/btn_rela_5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 5"
                    android:textAllCaps="false"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentBottom="true"/>

            </RelativeLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#eeeeee"
                android:padding="3dp"
                android:text="相对于Button 8布局"/>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp">

                <Button
                    android:id="@+id/btn_rela_6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 6"
                    android:textAllCaps="false"
                    android:layout_above="@id/btn_rela_8"
                    android:layout_toLeftOf="@+id/btn_rela_8"/>

                <Button
                    android:id="@+id/btn_rela_7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 7"
                    android:textAllCaps="false"
                    android:layout_above="@id/btn_rela_8"
                    android:layout_toRightOf="@id/btn_rela_8"/>

                <Button
                    android:id="@+id/btn_rela_8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 8"
                    android:textAllCaps="false"
                    android:layout_centerInParent="true"/>

                <Button
                    android:id="@+id/btn_rela_9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 9"
                    android:textAllCaps="false"
                    android:layout_below="@id/btn_rela_8"
                    android:layout_toLeftOf="@id/btn_rela_8"/>

                <Button
                    android:id="@+id/btn_rela_10"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 10"
                    android:textAllCaps="false"
                    android:layout_below="@id/btn_rela_8"
                    android:layout_toRightOf="@id/btn_rela_8"/>

            </RelativeLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#eeeeee"
                android:padding="3dp"
                android:text="相对于Button15布局"/>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp">
                <Button
                    android:id="@+id/btn_rela_11"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 11"
                    android:textAllCaps="false"
                    android:layout_toLeftOf="@id/btn_rela_15"
                    android:layout_alignTop="@id/btn_rela_15"/>
                <Button
                    android:id="@+id/btn_rela_12"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 12"
                    android:textAllCaps="false"
                    android:layout_above="@id/btn_rela_15"
                    android:layout_alignRight="@id/btn_rela_15"/>
                <Button
                    android:id="@+id/btn_rela_13"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 13"
                    android:textAllCaps="false"
                    android:layout_toRightOf="@id/btn_rela_15"
                    android:layout_alignBottom="@id/btn_rela_15"/>
                <Button
                    android:id="@+id/btn_rela_14"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 14"
                    android:textAllCaps="false"
                    android:layout_below="@id/btn_rela_15"
                    android:layout_alignLeft="@id/btn_rela_15"/>
                <Button
                    android:id="@+id/btn_rela_15"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button 15"
                    android:textAllCaps="false"
                    android:layout_centerInParent="true"/>

            </RelativeLayout>

        </LinearLayout>
    </ScrollView>


</LinearLayout>

RelativeLayoutActivity.java

public class RelativeLayoutActivity extends MyBaseActivity {

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

相关文章

  • 布局 - 相对布局

    RelativeLayout 相对布局 相对布局也是一种非常常用的布局,它可以通过相对定位的方式让控件出现在任何位...

  • android第四课。

    今天学习了两种布局方式。一种是线性布局,另一种是相对布局。 线性布局: 相对布局: 相对布局 图标要选择对齐方式

  • android基础

    布局基本布局 FrameLayout线性布局 LinearLayout相对布局 RelativeLayout绝对布...

  • Android之6大布局

    LineLayout (线性布局) RelativeLayout(相对布局) TableLayout(表格布局) ...

  • 2 布局

    LinearLayout(线性布局) RelativeLayout(相对布局) TableLayout(表格布局)...

  • 常用的五大布局

    常用的五大布局(线性布局,相对布局,帧布局,表格布局,绝对布局) 1,线性布局 LinearLayout ...

  • 深入理解Android布局就看这个!

    一、常见布局 线性布局——LinearLayout相对布局——RelativeLayout帧布局——FrameLa...

  • 安卓原生页面布局总结

    布局分为线性布局:LinearLayout和相对布局:RelativeLayout 线性布局:LinearLayo...

  • 安卓(android)六大布局详解

    线性布局:(LinearLayout) 相对布局:(RelativeLayout) 帧布局:(FrameLayou...

  • 基础布局

    Android中的布局 线性布局:LinerLayout 表格布局:TableLayout 相对布局:Relati...

网友评论

      本文标题:布局 - 相对布局

      本文链接:https://www.haomeiwen.com/subject/lxvagqtx.html