Android的布局

一、线性布局(LinearLayout)

在这里插入图片描述
在这里插入图片描述

注意:如果排列方向为horizontal,则不可以将宽度设置为match_parent,因为一个控件就占据了全部空间,其他控件就没有地方放了;
同理,如果排列方向为vertical,则不可以将高度设置为match_parent

layout_gravity属性设置布局

在这里插入图片描述
android:layout_weight属性:
允许使用比例的方式来指定控件的大小,在手机屏幕适配性上起很大作用,下面编写一个文本编辑框和一个发送按钮
将文本框和按钮的layout_weight的属性都设置为1,意思是两个控件各占二分之一宽度
在这里插入图片描述

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

在这里插入图片描述

仅仅指定EditText的layout_weight属性,将Button的宽度改为wrap_content,这时文本框将占据除按钮外的全部宽度
在这里插入图片描述

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

在这里插入图片描述

二、相对布局(RelativeLayout)

可以通过相对定位的方式让空间出现在布局的任意位置
1.相对于父布局定位
在这里插入图片描述

<Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="button1"/>
    <Button
        android:id="@+id/button_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="button2"/>
    <Button
        android:id="@+id/button_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="button3"/>
    <Button
        android:id="@+id/button_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="button4"/>
    <Button
        android:id="@+id/button_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="button5"/>

在这里插入图片描述
2.相对控件定位
需要注意的是基准的控件要写在最前面,不然会报错

<Button
        android:id="@+id/button_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="button2"/>

    <Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button_2"
        android:layout_toLeftOf="@+id/button_2"
        android:text="button1"/>

    <Button
        android:id="@+id/button_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button_2"
        android:layout_toRightOf="@+id/button_2"
        android:text="button3"/>
    <Button
        android:id="@+id/button_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button_2"
        android:layout_toRightOf="@+id/button_2"
        android:text="button4"/>
    <Button
        android:id="@+id/button_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button_2"
        android:layout_toLeftOf="@+id/button_2"
        android:text="button5"/>

在这里插入图片描述
3.另一种相对控件定位的属性

<Button
        android:id="@+id/button_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="button2"/>

    <Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button_2"
        android:layout_below="@+id/button_2"
        android:text="button1"/>

    <Button
        android:id="@+id/button_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button_2"
        android:text="button3"/>
    <Button
        android:id="@+id/button_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button_2"
        android:layout_toRightOf="@+id/button_2"
        android:text="button4"/>
    <Button
        android:id="@+id/button_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button_2"
        android:text="button5"/>

在这里插入图片描述

三、帧布局(FrameLayout)

没有方便的定位方式,所有控件都会默认摆放在布局的左上角
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.a102.uilayouttest.FrameActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!!!!!!!!!!!!"/>
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>
</FrameLayout>

在这里插入图片描述
由于都在左上角,文本被图片挡住了,这个是默认效果,可以使用layout_gravity属性来指定控件位置
在这里插入图片描述

在这里插入图片描述

四、百分比布局

允许直接指定控件在布局中所占的百分比
由于LinearLayout布局已经支持按比例指定控件大小了,所以这个布局用来扩展其他的两个布局。提供了PercentFrameLayout、PercentRelativeLayout两个全新的布局

首先添加依赖,在app->build.gradle里的dependencies下添加

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

在这里插入图片描述
编辑布局,PercentFrameLayout会继承FrameLayout布局,所有控件会摆放在左上角,所以采用gravity属性分开控件
在这里插入图片描述

<Button
        android:id="@+id/button_1"
        android:layout_gravity="left|top"
        android:text="Button1"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />
    <Button
        android:id="@+id/button_2"
        android:layout_gravity="right|top"
        android:text="Button2"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />
    <Button
        android:id="@+id/button_3"
        android:layout_gravity="left|bottom"
        android:text="Button3"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />
    <Button
        android:id="@+id/button_4"
        android:layout_gravity="right|bottom"
        android:text="Button4"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />

自定义View的xml布局文件中需要加入xmlns:前缀=http://schemas.android.com/apk/res/你的应用程序包路径

xmlns:app="http://schemas.android.com/apk/res-auto"

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值