2017Google Study Jams系列之课程1B-打造布局

@极简主义患者/社交控/伪技术宅/沉迷幻想不能自拔的文艺少年
不定期更新的文字平台:微博 简书

Viewgroups是包含了一组视图的视图,与视图一样有矩形边界和各种属性
包含其他视图的视图为父视图,被包含的视图则称为子视图,同被一个父视图包含的所有子视图称为兄弟视图

1. 线性布局(LinearLayout)

  • 例子:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orintentation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <TextView
            android:text="Guest List"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"/>
        <TextView
            android:text="Kunal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
        </LinearLayout>
    
        ```
    
    - 还记得1A课程中我们提到的**独立结束标签**和**自结束标签**吗?  
    在上面的示例代码中`<LinearLayout ...></LinearLayout>`就是一个独立结束标签,而在第一个**>**之前的代码属于线性组视图的标签属性,所以并没有**/**哦!
    - Google我们没见到过的属性:**android:orintentation**,在开发者文档里面找到有如下解释:
    > __android:orientation__  
    Should the layout be a column or a row? Use "horizontal" for a row, "vertical" for a column. The default is horizontal.  
    
    所以我们可以知道orintentation这个属性是用来控制线性布局的排列方向:**水平(horizontal)or垂直(vertical)**
    
    - 第一行属性xmlns是XML命名空间(namespace)声明,是为了防止属性名称冲突而需要遵守的.比如**A.width和B.width**就是两个拥有不同命名空间但同属性名的属性,有了命名空间后他们不会发生冲突
    
    
  • 布局要点:(宽度&高度/等权重子视图/布局权重)

    • 宽度&高度:在1A课程里我们也说过设定视图固定宽度和高度时使用dp作为单位,设定自动化宽高dp值使用wrap_content,在这里同样适用,而有了Viewgroup之后我们多了一种属性值叫match_parent,可设置子视图的宽度或高度与父视图一致.
    • 等权重子视图(Equally weighted children):设置子视图高度(或者宽度,相对于竖直和水平布局)属性为0dp,布局权重属性(layout_weight)为1
    • 布局权重即将屏幕进行比例分割,然后根据每个视图所设置的权重进行布局

2.相对布局(RelativeLayout)

  • 根据子视图与父视图的相对位置进行布局

    • 子视图与父视图的位置关系有四种:Left edge/Top edge/Right edge/Bottom edge,所以决定子视图位置的属性有:
    android:layout_alignParentTop="true"//false可缺省,下同
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    

    这些属性称为布局参数,而且可同时使用,从而实现子视图的各种摆放位置,下面为一个例子:

    <RelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:padding="16dp">
       <TextView
            android:text="I’m in this corner"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true" />
    
        <TextView
            android:text="No, up here"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true" />
    
        <TextView
            android:text="Wait, I’m here"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true" />
    
        <TextView
            android:text="Actually, I’m here"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true" />
    
    </RelativeLayout>
    
  • 根据子视图与兄弟视图的相对位置进行布局

    • 首先需要有锚点视图,即已经利用相对父视图固定好位置的子视图,其他子视图可以根据锚点视图进行布局,属性包括:
    //我们在进行相对定位时需要知道锚点视图的ID名称,ID名称是每个视图可定义的唯一的标识
    android:id="@+id/xx_xx"
    
    //在需要定位的视图属性加入如下属性即可相对"xx_xx"进行定位
    android:layout_toLeftof="@id/xx_xx"
    android:layot_above="@id/xx_xx"
    

    更多布局属性需要Google咯,下面给出视频中的例子:

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/lyla_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:textSize="24sp"
            android:text="Lyla" />
    
        <TextView
            android:id="@+id/me_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@id/lyla_text_view"
            android:textSize="24sp"
            android:text="Me" />
    
        <TextView
            android:id="@+id/natalie_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/lyla_text_view"
            android:textSize="24sp"
            android:text="Natalie" />
    
        <TextView
            android:id="@+id/jennie_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:textSize="24sp"
            android:text="Jennie" />
    
        <TextView
            android:id="@+id/omoju_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_above="@id/jennie_text_view"
            android:textSize="24sp"
            android:text="Omoju" />
    
        <TextView
            android:id="@+id/amy_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_above="@id/omoju_text_view"
            android:textSize="24sp"
            android:text="Amy" />
    
        <TextView
            android:id="@+id/ben_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:textSize="24sp"
            android:text="Ben" />
    
        <TextView
            android:id="@+id/kunal_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@id/ben_text_view"
            android:textSize="24sp"
            android:text="Kunal" />
    
        <TextView
            android:id="@+id/kagure_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/ben_text_view"
            android:textSize="24sp"
            android:text="Kagure" />
    
    </RelativeLayout>
    
  • 内边距(padding)和外边距(maigin)

    • 内边距的设定方法:
    android:padding="Ndp"
    or
    android:paddingLeft="Ndp"
    android:paddingRightt="Ndp"
    android:paddingTop="Ndp"
    android:paddingBottom="Ndp"
    
    
    • 外边距的设置需要在ViewGroup 中,设定方法:
    android:layout_margin="Ndp"
    or
    android:layout_marginLeft="Ndp"
    android:layout_marginRight="Ndp"
    android:layout_marginTop="Ndp"
    android:layout_marginBottom="Ndp"
    
    • 设置边距的值:
      Material Design指南中建议8dp的倍数
      如在App应用设计界面从左至右划三条线:**16dp/72dp/-16dp**
      做到排列有序,会使应用更容易阅读和使用
    • 给出视频中的例子:
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <ImageView
            android:src="@drawable/ocean"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scaleType="centerCrop" />
    
        <TextView
            android:text="You're invited!"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="16dp"   
            android:paddingLeft="16dp"
            android:paddingBottom="8dp"
            android:textColor="@android:color/white"
            android:textSize="45sp"
            android:background="#009688"/>
    
        <TextView
            android:text="Bonfire at the beach"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="16dp"
            android:paddingBottom="16dp"
            android:textColor="@android:color/white"
            android:textSize="24sp"
            android:background="#009688"/>
    
    </LinearLayout>
    

3.与Google的Kirill Grouchnikov聊天

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

推荐阅读更多精彩内容