UI布局初试three---四种基本布局

  • 版权声明:欢迎转载,转载请注明出处。 如果本文帮助到你,本人不胜荣幸,如果浪费了你的时间,本人深感抱歉。如果有什么错误,请一定指出,以免误导大家、也误导我。感谢关注。

什么是布局?

布局是一种可以放置很多控件的容器,他可以按照一定的规律调整内部控件的位置,从而编写出好看的界面。布局的内部也能够放置布局,形成多层嵌套。

线性布局 LinearLayout

LinearLayout 叫做线性布局,他会将其内部的控件在线性的方向依次排列。 他是按照属性android:orientation的指定,来决定其子视图设置为水平还是垂直。

  • horizontal 表示一行 (水平) 默认的值
  • vertical 表示一列 (垂直)

LinearLayout 是布局中的根视图,因此应将宽度和高度设置为 "match_parent",从而填满可供应用使用的整个屏幕区域。

栗子:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:layout_gravity="top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1" />

        <Button
            android:layout_gravity="bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"/>
    </LinearLayout>

android:layout_gravity属性:指定控件在布局中对齐方式,需要根据父视图的排列方式来指定,比如父视图对齐方式为vertical,只有在水平方向上的对齐方式才有效

然后就是LinearLayout的重要属性---android:layout_weight:一般称为权重。他是使用比例的方式来指定控件的大小。

栗子:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Input someing"/>
    
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Button"/>
    </LinearLayout>                                                                                                                                                                                    

首先把width的值指定为0,可以理解成宽度不再受width控制,而由weight来控制,系统会把所有的weight的值加起来,然后按份分配给各个控件。另外,我们也可以只指定其中一部分控件的weight值来实现UI效果。

相对布局 RelativeLayout

他可以通过相对定位的方式让控件出现在布局的任何位置。可以相对与父视图定位,也可以相对于控件定位

栗子:
    <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:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Button 1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="Button2 " />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Button 3" />
        
    </RelativeLayout>

android:layout_alignParentLeft:相对于父控件左对齐,其他的依次类推
android:layout_centerInParent:与父控件的中心对齐

 栗子:
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Button 3"/>
        
        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/btn3"
            android:layout_toLeftOf="@id/btn3"
            android:text="Button 1" />
        
        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/btn3"
            android:layout_toRightOf="@id/btn3"
            android:text="Button 2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/btn1"
            android:text="Button 4"/>
    </RelativeLayout>

android:layout_above:让一个控件位于其指定控件的上方,需要指定相对控件的id,其他的依次类推

android:layout_toLeftOf:让一个控件位于其指定控件的左侧

android:layout_alignLeft:让一个控件的左边缘与另外一个控件的左边缘对齐

这里我们需要注意的是:如果一个控件要引用另外一个控件的id时,该控件一定要定义在引用控件的后面,不然会找不到id。

帧布局 FrameLayout

FrameLayout是最简单的布局了。应用场景相对于前面两种来说也少了很多。 所有放在布局里的控件,都按照层次堆叠在屏幕的左上角。后加进来的控件覆盖前面的控件。

栗子:
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="50sp"
            android:textColor="#ff0000"
            android:text="Text"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </FrameLayout>

简而言之,他根本控制不了子控件在其内部的位置

百分比布局 Percent

可以不用match_parent或者wrap_content等方式来指定控件的大小, 而是直接指定控件在布局中所占的百分比,可以轻松实现按任意比例分配布局的效果。
LinearLayout已经可以使用权重来按比例分配控件大小了,所以只需要为FrameLayout和RelativeLayout提供扩展。

  • PercentFrameLayout 继承了FrameLayout的特性
  • PercentRelativeLayout 继承了RelativeLayout的特性

百分比布局属于新增的布局,我们要想使用他,就需要在项目下的build.gradle 文件添加百分比布局库的依赖。具体就是在dependencies闭包中添加以下内容:

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

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

由于PercentFrameLayout不是系统内置的布局,所以我们需要写出完整的路径,并且给他定义一个app的命名空间。所以我们可以使用app:layout_widthPercent与app:layout_heightPercent来指定控件的宽与高

好了,对于四种基本布局的基本用法暂时就了解到这里。慢慢用到新的知识再来分享。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容