我今天开始学习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>
使用此代码生成待办事项列表,这些项目都标记为“项目1”,“项目2”等。
我不明白当没有使用实际代码来创建列表时如何创建列表
- 此外,我想知道相关的XML编码在Android中的编程方式。如果它有用,我应该学习这门语言吗? 谢谢。
答案 0 :(得分:5)
好的,首先,让我解释一下当您使用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">
创建一个ViewGroup,在本例中为LinearLayout,它是一个视图持有者,根据方向“垂直”或“水平”排列元素,“fill_parent”属性要求保留父视图上的所有空间,如果这样元素parent是根视图,然后它将占用整个屏幕空间。
第二
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New To Do Item"/>
在此ViewGroup中创建一个与父级宽度匹配的EditText组件(在本例中为LinearLayout),并保持元素的高度与内容中的内容一致(这就是为什么它从单行开始,并且就像你一样写它可能会垂直增长...)
第三:
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
创建一个listView以垂直可滚动方式显示内容,如果没有使用适配器来填充某些内容,则可能未显示此元素,因为我们使用的是LinearLayout,此元素将显示在下面创建的EditText下方。 ..
问候!
答案 1 :(得分:1)
android图形布局设计器使用示例数据预先填充listview,以便了解它的外观。如果您要在模拟器或手机上运行此代码,则不会显示任何内容。
如果你想深入研究android编程,XML有点用处,幸运的是你可以在图形拖放设计器中做大部分事情。