Android布局控件之LinearLayout、RelativeLayout、GridLayout、ScrollView

线性布局(LinearLayout)

orientation

  • horizontal:水平从左往右
  • vertical:垂直从上到下
  • 若不指定orientation属性,默认为水平
<?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"
    android:orientation="vertical"
    tools:context=".LinearLayoutActivity"><!--tools只有在开发的时候才有用, 在这其实可以去掉-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="横排第一个    "
            android:textSize="17sp"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="横排第二个"
            android:textSize="17sp"
            android:textColor="#000000"/>

    </LinearLayout>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="竖排第一个"
            android:textSize="17sp"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="竖排第二个"
            android:textSize="17sp"
            android:textColor="#000000"/>

    </LinearLayout>

</LinearLayout>

在这里插入图片描述

线性布局的权重

  • 指的是线性布局的下级视图各自拥有多大比例的宽高
  • 权重属性名叫layout_weight,但该属性不在LinearLayout节点设置,而在线性布局的直接下一级视图设置,表示该下级视图占据的宽高比例。
  • layout_width填0dp时,layout_weight表示水平方向的宽度比例
  • layout_height填0dp时,layout_weight表示垂直方向的高度比例
<?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"
    android:orientation="vertical"
    tools:context=".LinearLayoutActivity"><!--tools只有在开发的时候才有用, 在这其实可以去掉-->

    <!--<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="横排第一个    "
            android:textSize="17sp"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="横排第二个"
            android:textSize="17sp"
            android:textColor="#000000"/>

    </LinearLayout>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="竖排第一个"
            android:textSize="17sp"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="竖排第二个"
            android:textSize="17sp"
            android:textColor="#000000"/>

    </LinearLayout>-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="横排第一个"
            android:textSize="17sp"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="横排第二个"
            android:textSize="17sp"
            android:textColor="#000000"/>

    </LinearLayout>

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:text="竖排第一个"
            android:textSize="17sp"
            android:textColor="#000000"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="竖排第二个"
            android:textSize="17sp"
            android:textColor="#000000"/>

    </LinearLayout>

</LinearLayout>

在这里插入图片描述

相对布局(RelativeLayout)

  • 相对布局的下级视图位置由其他视图决定。用于确定下级视图位置的参照物分两种:
    • 与该视图自身平级的视图
    • 该视图的上级视图(也就是它归属的RelativeLayout)
  • 如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角
    在这里插入图片描述
<?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="150dp">

    <TextView
        android:id="@+id/tv_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_centerInParent="true"
        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:background="#ffffff"
        android:layout_centerHorizontal="true"
        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:background="#ffffff"
        android:layout_centerVertical="true"
        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:background="#ffffff"
        android:layout_alignParentLeft="true"
        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:background="#ffffff"
        android:layout_alignParentRight="true"
        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:background="#ffffff"
        android:layout_alignParentTop="true"
        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:background="#ffffff"
        android:layout_alignParentBottom="true"
        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:background="#ffffff"
        android:layout_toLeftOf="@id/tv_center"
        android:layout_alignBottom="@id/tv_center"
        android:text="我在中间左边"
        android:textSize="11sp"
        android:textColor="#000000"/>
        <!--倒五行没有‘+’号,因为‘+id’代表是新的id
        倒四行保证上下对齐,不然会变到上面去。
        因为center只有一行,所以alignBottom和alignTop效果一样-->

    <TextView
        android:id="@+id/tv_above_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:layout_above="@id/tv_center"
        android:layout_alignLeft="@id/tv_center"
        android:text="我在中间上面"
        android:textSize="11sp"
        android:textColor="#000000"/>
        <!--倒四行保证左边对齐,不然会变到最左边去。-->

</RelativeLayout>

在这里插入图片描述

网格布局(GridLayout)

  • 支持多行多列
  • 默认从左往右、从上到下,(也就是说,先从左往右,再从上到下)新增两个属性:
    • columnCount属性,它指定了网格的列数,即每行可以放几个视图
    • rowCount属性,他指定了网格的行数,即每列可以放几个
<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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"
    android:columnCount="2"
    android:rowCount="2"><!--超过2 * 2 = 4个后它会继续向下排,行数设定为2其实没有很大意义-->

    <TextView
        android:layout_width="0dp"
        android:layout_columnWeight="1"
        android:layout_height="60dp"
        android:background="#ffcccc"
        android:text="浅红色"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"/>
    <!--gravity用来设置对齐方式,在这使其居中-->

    <TextView
        android:layout_width="0dp"
        android:layout_columnWeight="1"
        android:layout_height="60dp"
        android:background="#ffaa00"
        android:text="橙色"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"/>

    <TextView
        android:layout_width="0dp"
        android:layout_columnWeight="1"
        android:layout_height="60dp"
        android:background="#00ff00"
        android:text="绿色"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"/>

    <TextView
        android:layout_width="0dp"
        android:layout_columnWeight="1"
        android:layout_height="60dp"
        android:background="#660066"
        android:text="深紫色"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"/>

    <TextView
        android:layout_width="0dp"
        android:layout_columnWeight="1"
        android:layout_height="60dp"
        android:background="#660066"
        android:text="深紫色"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"/>

</GridLayout>

在这里插入图片描述

滚动视图(ScrollView)

  • 滚动视图有两种:
    • ScrollView:垂直方向,layout_width属性值设置为match_parent,layout_height属性值设置为wrap_content。
    • HorizontalScrollView:水平方向,layout_width属性值设置为wrap_content,layout_height属性值设置为match_parent。
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="200dp">

        <!--水平方向的线性布局,两个子视图的颜色分别为青色和黄色-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <View
                android:layout_width="300dp"
                android:layout_height="match_parent"
                android:background="#aaffff" />

            <View
                android:layout_width="300dp"
                android:layout_height="match_parent"
                android:background="#ffff00" />

        </LinearLayout>

    </HorizontalScrollView>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--垂直方向的线性布局,两个子视图的颜色分别为绿色和橙色-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <View
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="#00ff00" />

            <View
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="#ffffaa" />

        </LinearLayout>

    </ScrollView>


</LinearLayout>

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值