2017Google Study Jams之1A对View和ViewGroup的认识

此次活动的举办方:Google Study Jams活动官网

我的博客(同步此次活动笔记):CSDN博客我的简书
Google Developers

一、1A 认识View的笔记

1、初识Android

  • Android是一款开源的基于Linux的操作系统,主要用于移动设备(手机和平板),也用于汽车,家电等设备上来进入物联网时代。
  • 开发的硬件设备:Windows,Mac等
  • 开发的操作系统:Windows,Linux,Ubuntu等
  • 测试设备:Android手机、平板或其他Android系统设备
  • 集成开发环境:IDE(Android studio(推荐), Eclipse(Google目前不再维护更新))
  • Android Studio:Android Studio 是一个Android集成开发工具,基于IntelliJ IDEA。Android Studio 提供了集成的 Android 开发工具用于开发和调试。
  • 国内比较好的一个中文镜像网站

2、认识Android的View(视图)

  • View:View是屏幕上的一个矩形区域,是布局组件的基本组成元素。
  • Layout:所有的View视图拼起来就是屏幕上的Layout。
  • View有TextView(文本视图),ImageView(图像视图), Button(按钮)等。

3、认识XML

  • 使用Android studio来编写并预览
  • 编写屏幕上的layout,layout由之前认识的View组成
  • XML:Extensible Markup Language 可拓展标记语言,用来编写布局Layout
  • View的命名规则:采用驼峰规则(Camel-Case),也就是将每个单词的首字母大写,中间不加任何符号和空格
  • 标签:双标签或者自闭标签(例: 或)
  • 属性:“=”左边是属性名,右变属性值,每个属性都有默认值
  • 一个在线的视图预览工具

4、开启View的第一个控件:TextView(文本视图)

<TextView
        android:id="@+id/title_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/my_photos"
        android:textAppearance="?android:textAppearanceLarge"
        android:textColor="#4689C8"
        android:textStyle="bold" />
  • 单位:
    • dp:用于设置控件的宽高 (与手机像素密度无关)
    • sp:用于设置文本字体的大小 (与手机像素密度无关)
    • px :与手机像素有关(不推荐作为控件的宽高)
  • wrap_content:根据内容的大小决定控件的大小
  • android:textAppearance=”?android:textAppearanceLarge” 可以设置系统的属性,采用大中小字体样式
  • 颜色:引用系统或者本地资源(@color/white)或者采用16进制“#FFFFFF”
  • Common Android Views 常用基础控件模板供参考使用

5、ImageView (图像视图)

<ImageView
      android:id="@+id/photo_image_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:src="@drawable/icon"
      android:scaleType="centerCrop"/>
  • android:src:加载本地图片资源(Android studio对图片资源有一定的要求,建议使用.png的文件资源)
  • android:scaleType:对图片进行一定的裁剪,缩放和位置摆放的操作

6、通过看Android开发文档帮助开发

二、1B 认识ViewGroup的笔记

1、认识ViewGroup

  • ViewGroup相当于一个放置View的容器,并且我们在写布局xml的时候,会告诉容器(凡是以layout为开头的属性,都是为用于告诉容器的),例如宽度(layout_width)、高度(layout_height)、对齐方式(layout_gravity)等;当然还有后面要说的layout_margin、layout_padding等;所以一个ViewGroup是一个可以包含子View的容器,是布局文件和View容器的基类。

2、初识布局(Layout)-线性布局(LinearLayout)

  • 水平布局 horizontal
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    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:text="按钮2"/>   
     <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3"/>   
</LinearLayout>
  • 垂直布局 horizontal
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    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:text="按钮2"/>   
     <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3"/>   
</LinearLayout>
  • 两种方式效果对比

水平布局 垂直布局

  • 概念: LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列。
  • 属性:android:gravity:指定如何在该对象中放置此对象的内容(x/y坐标值)
    android:orientation:设置它内容的对其方向(横向/竖向)
    ps:gravity :这个英文单词是重心的意思,在这里就表示停靠位置的意思。

    • 当 android:orientation=”vertical” 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。
    • 当 android:orientation=”horizontal” 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。
  • 延伸:android:layout_gravity 和 android:gravity 的区别
    从名字上可以看到,android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠着换个属性设置,不过不设置默认是在左侧的。
    android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置。 比如说button: android:layout_gravity 表示按钮在界面上的位置。 android:gravity表示button上的字在button上的位置。

    • 可选值 :
      这两个属性可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。

3、相对布局(RelativeLayout)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    <ImageView
        android:id="@+id/image_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/image_center"
        android:text="我在左边"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_above="@id/image_center"
        android:text="我在上边"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/image_center"
        android:text="我在右边"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/image_center"
        android:text="我在下边"/>
</RelativeLayout>
  • 概念:RelativeLayout是一个允许子视图相对于其他兄弟视图或是父视图显示的视图组(通过ID指定)。每个视图的位置能够指定它相对于兄弟(比如在其他视图的左边或是下边)或是父视图(这里是指相对布局容器,比如底部对齐、中间偏左)的位置。
  • 属性:
    • android:layout_toLeftOf:位于相对控件的左方
    • android:layout_above:位于相对控件的上方
    • android:layout_toRightOf:位于相对控件的右方
    • android:layout_below:位于相对控件的下方
    • android:layout_centerVertical:在父布局中垂直居中
    • android:layout_centerHorizontal:在父布局中水平居中
    • android:layout_centerInParent:在父布局正中

相对布局效果图

4、优化布局-设定布局的内外边距(Paddding、Margin)

  • 简单概述:
    • android:layout_margin就是设置view的上下左右边框的额外空间
    • android:padding是设置内容相对view的边框的距离。
      通俗来说,一个设置内边距,一个设置外边距。
  • ps:在LinearLayout、RelativeLayout、TableLayout中,这2个属性都是设置都是有效的
    在FrameLayout中,android:layout_margin是无效的,因为FrameLayout里面的元素都是从左上角开始绘制的在AbsoluteLayout中,没有android:layout_margin属性。

内外边距说明

OK,关于View和ViewGroup的认识就记到这里了。感觉各位的阅读!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值