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的图形用户界面是使用层次结构ViewViewGroup对象以及对象类来构建的.View对象是一个是UI组件类诸如buttons 以及 text fields.
ViewGroup 对象是隐藏的View容器用来定义子views的布局方式,诸如graid(网格布局)以及 垂直列表布局.

Android 提供了一种XML”词汇表”来对应相应的ViewViewGroup子类所以你可以在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文件;

  1. 点击Project 窗口, 打开 res > values > strings.xml.
  2. 添加两个strings文件到你的工程类似这样:
  3. 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>

布局的当前设计是EditTextButton组件在一起,在输入的时候文本会和内容一样的大小,如图2所示.

图 2. The EditTextButton 组件其宽度设置为”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 .


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值