《第一行代码Android》学习总结第三章 常用布局使用方法

      布局是可以放置许多空间的容器,同时布局内部可以放置布局,进行布局的嵌套。

1、LinearLayout线性布局

     控件会在布局的线性方向依次排列。

android:orientation属性

    1)vertical:垂直方向排列

    2)horizontal:水平方向排列(默认)

android:layout_gravity与android: gravity的区别:

    android: gravity  用于指定文字在控件中的对齐方式。

    android:layout_gravity  用于指定控件在布局中的对齐方式。

        当排列方向为horizontal时,内部控件的宽度一般不能指定为match_parent,否则会将整个水平方向占满。

        当LinearLayout排列方式为horizontal时,只有垂直方向的对齐方式才会生效,因为水平方向控件长度不固定;当LinearLayout排列方式为vertical时,只有水平方向的对齐方式才会生效,因为垂直方向控件长度不固定。

android:layout_weight:

        使用比例的方式指定控件大小,在手机适配方面很有用。此时控件大小不再由android:layout_width或android:layout_height指定,所以一般在该方向上指定为0dp。

大小计算方式:

        系统会把LinearLayout方向上所有指定layout_weight值相加,按比例分配大小。

 

2、RelativeLayout相对布局

        通过相对定位的方式让控件出现在指定位置。相对于父布局进行定位对齐:

        android:layout_alignParentLeft
        android:layout_alignParentRight
        android:layout_alignParentTop
        android:layout_alignParentBottom

相对于控件进行定位对齐:

        android:layout_above
        android:layout_toLeftOf
        android:layout_below
        android:layout_toRightOf

让一个控件与另一个控件上下左右边缘对齐:

        android:layout_alignLeft
        android:layout_alignRight
        android:layout_alignTop
        android:layout_alignBottom

3、FrameLayout帧布局

        很少使用,因为控件定位方式欠缺。所有控件会默认摆放在布局左上角。

4、百分比布局

        只有LinearLayout布局可以使用android:layout_weight属性按比例指定控件大小。通过引入PercentFrameLayout与PercentRelativeLayou可使控件按比例指定大小。

1、app/build.gradle文件中,在dependencies闭包添加:

 compile 'com.android.support:percent:26.0.0-alpha1'

2、使用PercentFrameLayout布局,由于百分比布局并不是系统SDK中内置的所以要写出完整包路径,同时定义app命名空间使用app前缀的属性。

<android.support.percent.PercentFrameLayout
    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"
    >
    <Button
        android:id="@+id/button1"
        android:text="Button 1"
        android:layout_gravity="left|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />
    <Button
        android:id="@+id/button2"
        android:text="Button 2"
        android:layout_gravity="right|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />
    <Button
        android:id="@+id/button3"
        android:text="Button 3"
        android:layout_gravity="left|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />
    <Button
        android:id="@+id/button4"
        android:text="Button 4"
        android:layout_gravity="right|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />
</android.support.percent.PercentFrameLayout>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值