Android-(5)布局

线性布局LinearLayout

<?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:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"
    android:background="#fff"
>
</LinearLayout>

android:orientation属性可以设置为水平horizontal和垂直vertical

再线性布局中的控件可以使用layout_gravity属性设置控件对布局的对齐方式

线性布局中控件的layout_weight属性设置宽度或高度百分比布置。


相对布局RelativeLayout

它默认所有子控件都在左上角.

布局中控件可设置layout_centerInParent=”true”等类似属性进行相对于父布局的布置。

也可以使用layout_above=”@id/button03” 等类似的方式进行相对与其他控件的布局,要注意的是,作为参照物的控件要放在设置属性控件的前面。

简单测试:

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
         />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:text="按钮2"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="按钮3"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="按钮4"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮5"/>

</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="match_parent">

    <Button
        android:id="@+id/button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="按钮1"
        />

    <Button
        android:id="@+id/button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/button01"
        android:layout_above="@id/button01"
        android:text="按钮2"
        android:layout_toStartOf="@+id/button01" />

    <Button
        android:id="@+id/button03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/button01"
        android:layout_above="@id/button01"
        android:text="按钮3"
        android:layout_toEndOf="@id/button01" />
    <Button
        android:id="@+id/button04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/button01"
        android:layout_below="@id/button01"
        android:text="按钮4"
        android:layout_toStartOf="@id/button01" />

    <Button
        android:id="@+id/button05"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/button01"
        android:layout_below="@id/button01"
        android:text="按钮5"
        android:layout_toEndOf="@id/button01" />

</RelativeLayout>

帧布局FrameLayout

子控件可以使用layout_gravity=”right”来进行布置,用的比较少。


百分比布局

需要添加库依赖,不知道如何添加可查看文章: http://beyondcbz.xyz/?p=228

使用第一种方法搜索:com.android.support:percent:28.0.0

版本可能不一样,找到后添加就好了。

可以在和main文件夹同级的文件build.gradle

打开可以看到:

这就是添加的依赖。之后其他部分也会有添加依赖的操作。

百分比布局还有点问题,之后再看看。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值