Android Development for Beginners(1A)

Lesson 1A Building Layouts

05 - Views

layout

The layout of an app is the design or arrangement of what the user sees on the screen. This user interface is composed of rectangular areas called Views. Big Views can contain smaller Views, and there is always a biggest View that contains all the others.

Paste_Image.png

User Interface

The user interface of an app is what we see on the screen of the Android device. It consists of one or more rectangular areas called Views, which display information. Some of the Views — the buttons, for example — also respond to a touch.

TextView

A View is a rectangular area on the screen. One type of View is a TextView, which displays one or more lines of text.
A TextView on the screen is drawn by a Java object inside the Android device. In fact, the Java object is the real TextView. But when talking about what the user sees, it’s convenient to refer to the rectangular area on the screen as the “TextView”.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:textSize="24sp"
    android:text="Hello"/>
Paste_Image.png

ImageView

  • DESCRIPTION:A View is a rectangular area on the screen. One type of View is anImageView, which displays an image such as an icon or a photograph.
    An ImageView on the screen is drawn by a Java object inside the Android device. In fact, the Java object is the real ImageView. But when talking about what the user sees, it’s convenient to refer to the rectangular area on the screen as the “ImageView”.
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/cake"
    android:scaleType="centerCrop"/>
Paste_Image.png
  • center vs centerCrop


    match_parent centerCrop.PNG

    match_parent center.PNG

    wrap_content centerCrop.PNG

    wrap_content center.PNG

Button

A View is a rectangular area on the screen. One type of View is a Button, which displays a piece of text. When touched, a properly configured Button tells the Android device to execute a method — a list of instructions, like a little program.
A Button on the screen is drawn by a Java object inside the Android device. In fact, the Java object is the real Button. But when talking about what the user sees, it’s convenient to refer to the rectangular area on the screen as the “Button”.

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:background="#00FF00"
    android:text="Force stop"
    android:onClick="forceStop"/>

![Upload Paste_Image.png failed. Please try again.]

Camel case

A computer is a machine that follows a list of instructions called a program. An Android device is a computer and an app is a program.
We often name the things inside the device with a two-word phrase such as “linear layout” or “main activity”. But our language Java does not permit a space between the two words of a name. When eliminating the space, we capitalize the second word so we can see where one word ends and the next one begins. This typographic convention is called camel case, and it yields many of the words that give Android programming its distinctive flavor: LinearLayout, MainActivity, onClick.

Paste_Image.png

XML

XML stands for “Extensible Markup Language”. It is a notation for writing information structured as a hierarchy or family tree. Examples include a Roman numeral outline of topics; a corporate organizational chart of divisions and subdivisions; or a listing of states, counties, and cities.
A state can contain many counties, and a county can contain many cities. But each city can be contained in only one county, and each county can be contained in only one state. In XML, we say that each item of data can contain many children, but can be contained in only one parent.
This family-tree structure makes XML ideal for describing the layout of the screen of an Android app, which is composed of rectangular areas calledViews. The layout always consists of one big View which may contain smaller ones, which may in turn contain even smaller ones.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:padding="8dp"
        android:text="Hello"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/mountains"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:background="#3A3A3A"
        android:text="Press me"
        android:onClick="doSomething"/>
</LinearLayout>
Paste_Image.png

12 - XML Syntax

XML Tag

Paste_Image.png

The Extensible Markup Language (XML) is a notation for writing a file containing pieces of information called elements. To indicate where an element begins and ends, we write tags. A tag is easy to recognize because it always begins and ends with the characters < and >. A tag also contains the name of the element (i.e. LinearLayout) whose beginning and end it marks.
An element often consists of a pair of tags, plus all the content between them. In this case, the second tag of the pair begins with the characters </ and we say that the second tag closes the first.
An element that does not need to enclose any content can consist of a single tag. In this case, the tag ends with the characters /> and we say that it is a self-closing tag.

Attributes

The Extensible Markup Language (XML) is a notation for writing a file containing pieces of information called elements. For example, a file describing the layout of the screen might contain elements representing buttons and images. The start of each element is marked with a tag surrounded by the characters < and >. A small element might consist of nothing but this tag.
An element can have smaller pieces of information called attributes written inside of its initial tag. Each attribute consists of a name and a value. For example, a TextView element might have an attribute whose name is “text” and whose value is “Hello”. We write the name of the attribute on the left, the value on the right, and join them with an equal sign. The value is always enclosed in double quotes.

Paste_Image.png

13 - Change the TextView

dp (Density-Independent Pixel)

The screen of an Android device is made of rows and columns of glowing dots called pixels. Devices can range in screen density, which means how many pixels per inch (or dots per inch) are on the screen. For example, an mdpi (or medium density device) has 160 dots per inch, while an xxhdpi (extra extra high density device) has 480 dots per inch.
If we specify the size of views in pixel values, then views would appear very small on the higher density devices, where there are many pixels packed into a small area. If a button is too small, then it would be hard for the user to touch it.
To achieve a consistent physical size of Views, across devices of different screen densities, we use a unit of measure called a density-independent pixel (dp or dip, pronounced “dee pee” or “dip”). 1 dp is equal to 1 pixel on an mdpi device. 1 dp is equal to 3 pixels on an xxhdpi device, and so on for other devices. Per Material design guidelines, any touch target on the screen should be at least 48 dp wide by 48 dp tall. That way, a button in an app on one device will be approximately the same physical size as that button in the same app running on a device with a different screen density.
Android devices will automatically handle the conversion from dp to pixel values, so developers just have to use dp values when specifying measurements in their layouts. For example, dp’s can be used to specify the width and height of a View, shown in the illustration.

Paste_Image.png

14 - Getting Past Errors

Link to the Common Views Cheatsheet

Paste_Image.png

How to take a screenshot
Android Studio enables you to capture a screenshot or a short video of the device screen while your app is running. Screenshots and videos are useful as promotional materials for your app, and you can also attach them to bug reports that you send to your development team.

15 - Setting Wrap Content

Hard coding
A computer is a machine that follows a list of instructions called aprogram. An Android device is a computer and an app is a program.
One way to get information into an app is to write it in the instructions of the app:
add 10 + 20
or make the TextView 100dp wideThis is called hardcoding the information into the app.
The TextView in the above instruction is an example of a View, which is a rectangular area on the screen that can display information. A View can be contained in a larger View, called its parent. Assuming that the TextView is contained in a parent whose width is 100dp, we might write the above instruction if we wanted the TextView to occupy the entire width of its parent. One disadvantage of hardcoding the value 100dp into this instruction is that we would have to remember to rewrite it if we ever changed the width of the TextView’s parent.
This is one reason why we avoid writing hardcoded values. There would be less for us to remember to do if we could write an instruction that would tell the TextView to get its width automatically from its parent. Less maintenance means fewer bugs.

Paste_Image.png
<!-- This TextView has its width hardcoded into it. -->
<LinearLayout
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Hello"/>
</LinearLayout>

<!-- This TextView gets its width from the parent that contains it. -->
<LinearLayout
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World"/>
</LinearLayout>

wrap_content

A View is a rectangular area on the screen, usually containing some content. For example, a TextView contains text, an ImageView contains an image, and a special type of View called a ViewGroup contains smaller Views inside of it.
We can specify the width or height of a View as a given distance. Alternatively, we can specify it as the special value wrap_content to shrink-wrap the View around its content. To prevent the View from wrapping itself too tightly, we can also specify a certain amount of padding.

Paste_Image.png

Paste_Image.png
<TextView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="#C0C0C0"
    android:text="Hello"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#C0C0C0"
    android:text="Hello"/>

16 - TextView Text Size

scale-independent pixel(sp)

DESCRIPTION: A scale-independent pixel(sp) is a unit of length for specifying the size of a font of type. Its length depends on the user’s preference for font size, set in the Settings app of the Android device.To respect the user’s preferences, you should specify all font sizes in scale-independent pixels. All other measurements should be given in device-independent pixels(dp’s).

Google Material Design

17 - TextView Text Color

Hexadecimal Colors

Hexadecimal color values are supported in all major browsers.
A hexadecimal color is specified with: #RRGGBB, where the RR (red), GG (green) and BB (blue) hexadecimal integers specify the components of the color. All values must be between 00 and FF.
For example, the #0000FF value is rendered as blue, because the blue component is set to its highest value (FF) and the others are set to the lowest value (00).

Color palette(调色板)

This color palette comprises primary and accent colors that can be used for illustration or to develop your brand colors. They’ve been designed to work harmoniously with each other.
The color palette starts with primary colors and fills in the spectrum to create a complete and usable palette for Android, Web, and iOS. Google suggests using the 500 colors as the primary colors in your app and the other colors as accents colors.

<ImageView
    android:src="@drawable/cake"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="center"/>

18 - Simple ImageView

<ImageView
    <!-- This is saying that the source for this image should be found in this file here.The file name is called cake. We use the at symbol to say that we're referencinga resource in the Android app, and drawable is the resource type.A drawable is like a graphic that will be shown on screen in Android.And again, cake is the file image name. -->
    android:src="@drawable/cake" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    <!-- Now we have the width and height, which are set to be wrapped content. So that the image view is only as big as the cake image inside of it. -->
    <!-- Lastly, we have this attribute called Android:scaledType and it's set to be center. Scale type tells the device how to scale up or scale down, based on the bound of the ImageView. When we say scaleType,it doesn't change the size of the image, it just centers it.So we see that the cake image is actually really big,but this is the center of the cake image.-->
    <!--We can set other values for scaleType, such as centerCrop. This scales down the image to fit the height and width of the view. In this case, we're constrained by the sides of the screen. We also maintain the aspect ratio of the original image so it doesn't get distorted.-->
    android:scaleType="center"/>

When displaying photographs, I like to use center crop. This is because it's typically okay to crop off the edges of the photograph in order to achieve this edge to edge look. When images go edge to edge without any white border or anything, this is what we call a full bleed image.

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

推荐阅读更多精彩内容