【安卓】四类布局

①LinearLayout 线性布局

使所包含的控件在线性方向上排序

分为垂直方向(android:orientation="vertical")和水平方向(android:orientation="horizontal")

a.对于android:layout_gravity属性(用于指定文字在控件中的对齐方式)

       horizontal时垂直方向上的对齐方式才生效,同理,vertical时水平方向下的对齐才生效

b.对于android:layout_weight属性

       此时,即使将width设为0dp ,控件的宽度不再由layout_width决定

系统将layout_weight值相加,得到一个总值,然后每个控件所占的大小的比比例就是控件/总值所得

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:hint="Type something"/>
    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Send"/>

 

图像如下:

②RelativeLayout 相对布局

可通过相对定位的方式让控件出现在布局的任意位置

        相对于控件进行定位

    <EditText
        android:id="@+id/edit_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:hint="Type something"/>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/edit_1"
        android:layout_toLeftOf="@id/edit_1"
        android:text="Send"/>

显示如下:

注意:当一个控件去引用另一个控件的id时,该控件一定要定义在引用控件的后面,不然早不到id

③FrameLayout帧布局

没有定位方式,所有控件都默认摆放在布局的左上角,多控件会重叠,应用场景较少

可通过layout_gravity属性来指定控件在布局中的对齐方式

 

以上三种布局只有LinearLayout支持使用layout_weight属性实现按比例指定控件大小的功能

 

④百分比布局

此布局中,不可使用wrap_content、match_parent等方式指定控件大小,而是允许直接指定控件在布局中所占的百分比,以实现任意比例分隔布局

百分比布局只为FrameLayout和RelativeLayout进行功能扩展,提供了PercentFrameLayout和PercentRelativeLayout两个布局

注意:

百分比布局定义在support库当中,在app/build.gradle中添加百分比布局库依赖

compile 'com.android.support:percent:24.2.1'

修改后点击右上角SyncNow进行同步

<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:layout_gravity="right|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        android:text="Button 2"/>
</android.support.percent.PercentFrameLayout>

显示如下:

 

 

 

注意:安卓部分的学习皆为郭霖《第一行代码》中的练习,真的是本好书,讲解细致,很适合新手入门,力荐

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值