Android Study
From: Android
Android 第一弹.
安装Android studio
width="640" height="498" src="http://v.qq.com/iframe/player.html?vid=h0338engksr&tiny=0&auto=0" allowfullscreen="">
构建一个简单的用户界面
在这一节中,你将创建一个包含一个文本字段和一个按钮的XML格式布局文件.在下一个章节中,当按下按钮发送带文本内容的字段到另一个Activity后你的app可以响应此动作;
Android app的图形用户界面是使用层次结构View和ViewGroup对象以及对象类来构建的.View对象是一个是UI组件类诸如buttons 以及 text fields.
ViewGroup 对象是隐藏的View容器用来定义子views的布局方式,诸如graid(网格布局)以及 垂直列表布局.
Android 提供了一种XML”词汇表”来对应相应的View 和 ViewGroup子类所以你可以在XML布局文件中使用并定义你的UI元素.
布局子类ViewGroup.
Layouts are subclasses of the ViewGroup. 在本节练习中,你会使用 LinearLayout布局方式.
图 1. 图解 ViewGroup 对象是如何形成的分支层次结构布局并包括其它View对象的说明
创建一个线性布局
添加一个文本标签
在 activity_main.xml 文件, 内部 元素, 添加下面的 元素:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><EditText android:id="@+id/edit_message"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="@string/edit_message" />
</LinearLayout>
这里描述了一个你注释的属性:
android:id
这里提供了一个视图的唯一标识符,你可以使用它来从您的应用程序的代码引用该对象,譬如阅读或者操作一个对象(在下一个章节中,你将看到),紧随其后的是类型资源(id
在这个场合),一个斜线,
at符号(@
)是当你引用任何来着XML文件中的资源对象时候需要的标识符.
The at sign (@
) is required when you’re referring to any resource object from XML. It is followed by the resource type (id
in this case), a slash, then the resource name (edit_message
).
android:layout_width
and android:layout_height
Instead of using specific sizes for the width and height, the "wrap_content"
value specifies that the view should be only as big as needed to fit the contents of the view. If you were to instead use"match_parent"
, then the [EditText][52]
element would fill the screen, because it would match the size of the parent [LinearLayout][51]
. For more information, see the Layouts guide.
android:hint
This is a default string to display when the text field is empty. Instead of using a hard-coded string as the value, the "@string/edit_message"
value refers to a string resource defined in a separate file. Because this refers to a concrete resource (not just an identifier), it does not need the plus sign. However, because you haven’t defined the string resource yet, you’ll see a compiler error at first. You’ll fix this in the next section by defining the string.
资源对象
一个资源对象是唯一标识app资源相关的名称,譬如位图,布局文件,或string.
在你的工程中任意资源都可以被定义成为一个资源对象在R.java
文件中.你可以使用对象名称来命名类参考资源,譬如当你需要指定一个字符串只通过这个android:hint
属性标签.你可以创建一个属性资源IDsandroid:id
用来和你的view关联起来,它允许你从其它代码引用此view.
当您编译app的时候SDK工具会生成R.java
文件.你不应该手动修改此文件.
更多信息,请阅读指南的Providing Resources章节.
添加一个字符串资源
默认情况下,你的Android 工程包括一个string资源文件位于res>values>strings.xml.这里,你将添加两个新的strings文件;
- 点击Project 窗口, 打开 res > values > strings.xml.
- 添加两个strings文件到你的工程类似这样:
- 3.
<?xml version="1.0" encoding="utf-8"?>
<resources><string name="app_name">My First App</string><string name="edit_message">Enter a message</string><string name="button_send">Send</string>
</resources>
在用户界面的文本中,总是指定每个string是一种资源.String资源允许你在一个位置管理所有的UI文本内容,使得文本内容便于更新和查找.外部string还允许您将为每个字符串资源提高不同定义来使得应用程序国际化.
更多关于使用string资源使您的app国际化的信息,请参考 Supporting Different Devices章节.
添加一个按钮
回到activity_main.xml文件中然后添加一个按钮后,你的文件应该是类似这样的:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><EditText android:id="@+id/edit_message"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="@string/edit_message" /><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_send" />
</LinearLayout>
布局的当前设计是EditText 和 Button组件在一起,在输入的时候文本会和内容一样的大小,如图2所示.
图 2. The EditText 和 Button 组件其宽度设置为”wrap_content”.
这样的方式对于按钮是很好的,但是文本标签并不是很好,因为用户可能输入的文本内容会很长.假如使用文本字段可以填充未使用的屏幕区域的设计就是比较好的,你可以在里面做一个LinearLayout可以随着长度的变化而变化,指定这种布局方式可以使用android:layout_weight属性.
权重值是一个数字被用来指定每一个view应该占用的空间比,relative布局是指的兄弟姐妹的总数.这种布局方式有点像饮料中的各种成份的分配:”2杯苏打水,1杯糖水”这样意味着你的喝的饮料2/3是苏打水.例如,如果你响给一个View2个权重,另外一个view1个权重,总数为3,因此,第一个view填充2/3的剩余空间,第二个view填充剩余的空间.如果你响添加第三个View并且至少给1个权重,那么第一个view(2个权重现在填充空间为1/2,而剩下的两个view各自填充1/4.
所有view默认的权重为0,所以你如果指定仅仅只有一个view上权重超过0,那么view填充剩余空间占比是在所有的view空间分配以后的剩余空间.
使输入框的长度填充整个屏幕的宽度
在 activity_main.xml, 修改 修改成类似于下面这样子:
<EditText android:id="@+id/edit_message"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content"android:hint="@string/edit_message" />
Figure 3. The EditText widget is given all the layout weight, so it fills the remaining space in the LinearLayout.
Here’s how your complete activity_main.xmllayout file should now look:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"> android:id="@+id/edit_message" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="@string/edit_message" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" />
运行你的App
在设备或者仿真器上来查看你的应用程序, 在工具栏上点击Run .
Android Study相关推荐
- Android Study Material Design 十 再探沉浸式
LZ-Says: 这些年,身边的"兄弟"越来越多, 真正的兄弟越来越少... 前言 今天,我们不讲美女,不讲三国,那么我们一起来聊聊有关Android中沉浸式实现. 关于写这篇文章 ...
- Android Study之自定义View进阶路:掌握绘制基本图形(一)
LZ-Says:我觉得人与人相处,首先排在首位的是诚信,其次才是一些其他的东西.而关于技术,交流才是成长的路,闭门造车永远看不到外面的精彩,领悟不到自身新高度.So,诚信待人,多交流,多沟通,一起成长 ...
- Android Study Jam 在线答疑第三期:Jetpack Compose 案例实战
Android Study Jam 火热进行中 感兴趣的小伙伴都上车了吗? 为助力大家打好 Jetpack Compose 基础 我们的第三场 Office Hours 在线答疑 将于 今天上午 10 ...
- Android Study 之 如何通过Data Binding提升撸码逼格(进阶篇)
LZ-Says:每一次的选择,都将面临更多的希望,加油,而今迈步重头跃... 前言 前段时间,完结了一篇Data Binding前篇,地址如下: Android Study 之 如何通过Data Bi ...
- android 自定义 snackbar,Android Study Material Design 五 之:自定义Toast以及玩转SnackBar...
> LZ-Says:有时候真的不需要让自己在意太多,在意的太多,反而有些累,计较太多,反而让自己患得患失,随心随性就好,愿各位伙计能开心撸码~ > LZ-Says:有时候真的不需要让自己在 ...
- Android Study 之 findViewById 变迁之路
现在,一到周六,想想还要继续上班,瞬间千万个不爽席卷而来,XXOO此处省略n字... 休息一天的好处在于,让你感觉一天休息时间格外珍贵,而坏处在于,劳资TMD还没休息过来,结果第二天又开始上班了,而且 ...
- Android study week1
前言 前言被阉掉了.虫合虫合虫合... 想看完整版的: https://segmentfault.com/n/1330000004531673 博客工具 写博客的时候对格式有一定要求,需要学会使用ma ...
- android gson工具包,Android Study 之 彻底解决 Gson解析 异常
> LZ-Say:大风呼呼的刮,就好像妖精要来的前奏一样~ > > 前言 开发过程中,App常用的接收参数的时候,几乎大部分都是以json格式为主,那么有关解析json的方式有很多, ...
- Android Study 之真正解决TextView字间距,那些扯淡的边儿去吧
LZ-Says:从个人的角度来说为什么要写博客,一方面可以拓展知识,通过分享去了解更多的知识:二方面可以通过优质的博文去推广自己. 写博客,就好像工作一样,要认真,要对自己写的东西负责,更要对别人看的 ...
最新文章
- 云网融合 — 电信网络云
- TCP/IP详解--学习笔记(1)-基本概念
- sdn智能互联系统及开发平台_聊天交友平台系统APP开发
- 1.在VS2010中文版中开发WP7程序
- 阿里开源的Arthas竟然还不会用?最佳实践来了!
- Android 控件之RatingBar评分条
- RabbitMQ 基本使用(注解的方式)
- python小孩子学有什么用_小孩子为什么一定要学习Python编程
- 全网最全 Chrome浏览器插件推荐大全 持续更新中
- C# Winform SplitContainer组件创建侧边菜单
- 【LeetCode】592. 分数加减运算
- html标签中添加点击事件的写法
- 家庭光纤宽带延长光纤
- 阿里一面集合_阿里天猫一面+阿里饿了么一面+阿里妈妈一面
- AMD首款5纳米PC处理器锐龙7000亮相,频率首破5GHz大关,单核性能提升15%
- 抓取2021百科知识竞赛题库和答案
- linux中默认文件666和目录777的权限关系
- 力天创见仓库客流管控方案
- “机器学习就是现代统计学”
- torch.squeeze 函数运用