Android开发从零开始(2)--2016.01.18

来源:互联网 发布:me3630 软件 用户手册 编辑:程序博客网 时间:2024/06/06 19:11

跟着官方文档来到了第二节,对于原来学习过的我来说还是很简单的,
主要介绍的是简单的桌面XML空间的写法和简单的属性,包括
-EditText
-id
-layout_weight
-layout_height
-layout_width
-hint
-Button
-text

别的不说,上代码:


EditText
android:id=”@+id/edit_message”
android:layout_weight=”1”
android:layout_width=”0dp”
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”

这里有几个东西需要说明一下,我原来跟着网上的视频学习的时候从来没有讲的这么详细过

1.关于ID

这是定义View的唯一标识符。可以在程序代码中通过该标识符对对象进行引用,例如对这个对象进行读和修改的操作(在下一课里将会用到)。

当想从XML里引用资源对象的时候必须使用@符号。紧随@之后的是资源的类型(这里是id),然后是资源的名字(这里使用的是edit_message)。

+号只是当你第一次定义一个资源ID的时候需要。这里是告诉SDK此资源ID需要被创建出来。在应用程序被编译之后,SDK就可以直接使用ID值,edit_message是在项目gen/R.java文件中创建一个新的标识符,这个标识符就和EditText关联起来了。一旦资源ID被创建了,其他资源如果引用这个ID就不再需要+号了。这里是唯一一个需要+号的属性。

2.关于weight

LinearLayout使用权重属性来达到这个目的,你可以使用android:layout_weight属性来设置。

权重的值指的是每个部件所占剩余空间的大小,该值与同级部件所占空间大小有关。就类似于饮料的成分配方:“两份伏特加酒,一份咖啡利口酒”,即该酒中伏特加酒占三分之二。例如,我们设置一个View的权重是2,另一个View的权重是1,那么总数就是3,这时第一个View占据2/3的空间,第二个占据1/3的空间。如果你再加入第三个View,权重设为1,那么第一个View(权重为2的)会占据1/2的空间,剩余的另外两个View各占1/4。(请注意,使用权重的前提一般是给View的宽或者高的大小设置为0dp,然后系统根据上面的权重规则来计算View应该占据的空间。但是很多情况下,如果给View设置了match_parent的属性,那么上面计算权重时则不是通常的正比,而是反比,也就是权重值大的反而占据空间小)。

对于所有的View默认的权重是0,如果只设置了一个View的权重大于0,则该View将占据除去别的View本身占据的空间的所有剩余空间。因此这里设置EditText的权重为1,使其能够占据除了按钮之外的所有空间。

这本来是个很简单的东西,在查看activity_main.xml文件的时候,我发现了一些我原来学习的时候没有见过的部分

tools:context=”com.example.zwb.myapplication.MainActivity”
相关文档中对这个属性的描述是这样的:
This attribute is typically set on the root element in a layout XML file, and records which activity the layout is associated with (at designtime, since obviously a layout can be used by more than one layout). This will for example be used by the layout editor to guess a default theme, since themes are defined in the Manifest and are associated with activities, not layouts. You can use the same dot prefix as in manifests to just specify the activity class without the full application package name as a prefix.

这通常是一个写在根元素的上的属性,并且记录着与这个XML相关的activity。

tools:showIn=”@layout/activity_main”>
Attribute set on the root element of a layout that ‘ed by another layout. This allows you to point to one of the layouts which includes this layout, and at designtime this included layout will be rendered with the outer layout surrounding it. That allows you to view and edit the layout “in context”. Requires Studio 0.5.8 or later.

这是一个被写在根元素的上的属性,用来表示当前的layout被哪个layout包含,允许查看上下文

0 0
原创粉丝点击