我今天开始学习Android开发,我需要帮助理解一些XML代码的工作原理.我正在使用的书没有解释代码,但是,使用它给了我想要的结果.
<?xml version="1.0" encoding="utf-8"?>
<inearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New To Do Item"
/*The above lines are the only things I understand. the code below does not create an
actual list, instead just initiates the layout of height and width. */
/>
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
使用此代码生成一个待办事项列表,这些项目都标记为"项目1","项目2"等.
我不明白如果没有使用实际代码来创建列表
- 另外,我想知道XML编码与Android编程有多相关.如果它有用,我应该学习这门语言吗?
谢谢.
好的,首先,让我解释一下当您使用xml资源时,android为您做了什么.
Android使用"Inflate"技术,其中包括基于XML标签创建Java对象,基本上它是基于标签和属性(xml)创建类和属性(java),先前的代码要求操作系统:
第一:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
Run Code Online (Sandbox Code Playgroud)
创建一个ViewGroup,在这种情况下是一个视图持有者,它根据方向"垂直"或"水平"排列元素,"fill_parent"属性要求保留在父视图上的所有空间,如果这个元素父元素是根视图,然后它将占用整个屏幕空间.
第二:
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New To Do Item"/>
Run Code Online (Sandbox Code Playgroud)
在此ViewGroup中创建一个与父级宽度匹配的EditText组件(在本例中为LinearLayout),并保持元素的高度与内容的长度一致(这就是为什么它从1个单行开始,并且当你写它时可能垂直生长...)
第三:
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
创建一个listView以垂直可滚动方式显示内容,如果没有使用适配器来填充某些内容,则可能未显示此元素,因为我们使用的是LinearLayout,此元素将显示在下面创建的EditText下方...
问候!
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |