Android的布局

布局:布局是存放控件的容器,并且可以规定控件在容器里的摆放顺序,布局里可以存放控件,当然,布局里也可以嵌套布局。

一、线性布局(LinearLayout)(线性方向上依次排列)
1、android:orientation=“vertical”(竖直方向上的线性)
android:orientation=“horizontal”(水平方向上的线性)
2、布局中的控件的属性
(1)android:gravity(控件中的文字在控件中的对齐方式)
(2)android:layout_gravity(控件在布局中的对齐方式)
3、如果是vertial,那么控件的高度就不可以是“match_parent”,而且控件对齐是在水平方向上生效的
如果是horizontal,那么控件的宽度就不可以是“match_parent”。而且控件对齐是在垂直方向上生效的
4、android:layout_weight (按照比例分布控件的宽度)
(1)此时控件的宽度不再由android:layout_width控制,此时可以把layout_width置为0,但是不可以不写
(2)布局中会根据其他控件的layout_weight的比例来分布宽度
控件A layout_weight=“3”
控件B layout_weight=“1”
所以控件A占整个布局宽度的3/4,控件B占整个布局宽度的1/4
二、相对布局(RelativeLayout)
1、控件可以相对于布局任意摆放)

//左上
alignParentLeft="true"
alignParentTop="true"
//正中间
centerInParent="true"
还有左下,右上,右下就不写了

2、相对于某个控件布局(参照物控件必须定义在最前面)
layout_above="@id+参照物控件名" (参照物之上)
layout_below="@id+参照物控件名" (参照物之下)
layout_toRightOf="@id+参照物控件名" (参照物之右)
ayout_toLeftOf="@id+参照物控件名"(参照物之左)
layout_alignRight (依次是右,左,上,下边缘对齐)
layout_alignLeft
layout_alignTop
layout_alignBottom
三、帧布局(FrameLayout)(用的很少,碎片会用到)
就是全放在左上角
四、百分比布局(PercentFramLayout,PercentRelativeLayout)
百分比就是不用wrap_content和match_parent来表示长宽。(而是允许控件在布局中直接占百分比)又因为LinearLayout已经可以用layout_weight来进行百分比。所以百分比布局只要有(PercentFramLayout和PercentRelativeLayout)。其实她们就是FramLayout和RelativeLayout,他们两个具有的特性还是存在的,只不过在此基础上添加了百分比
以PercentFramLayout为例,PercentRelativeLayout相似

<?xml version="1.0" encoding="utf-8"?>
<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>

(1)百分比需要添加依赖
(2)相当于四个控件田字均匀分布
五、CoordinatorLayout(加强版的FramLayout)(由Design Support库提供)

特点:这个布局可以监听所有其他子控件的各种事件,然后自动帮助我们做出最为合理的响应。在Material Design那里介绍悬浮按钮和可交互提示的时候有介绍
Material Design

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值