简介:ListView组件是在Android APP开发中经常用到的组件,主要是用来显示列表以及相应按下触发完成界面的跳转。
xml文件在ListView中的使用:
xml的作用:xml文件在Android开发中往往承担了界面的布局的作用,其中包含了各个组件以及它们在界面上存储的位置;同时比如strings.xml文件可以配置activity中用到的字符串,dimens.xml文件可以配置边界的位置参数。MainActivity也有属于它的布局文件,其中ListView是其中的一个组件。xml文件类似于给具体内容的实现框定了一个格式,它规定了ListView中数据填充的形式,实现了M(模型)和V(视觉)的分离。
activity_main.xml:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.asynctask.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="30sp"
android:text="@string/title1" />
<!-- ListView 由item.xml里的TextView给值 。-->
<ListView
android:id="@+id/listView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
/>
</RelativeLayout>
效果图:
分析:布局文件的ListView中第一行代码为它申请了唯一的id标识,第二、三行代码表示它的大小可以自适应内容的大小而变化,第四行体现了相对布局的特点,ListView放在文本textView的下方。ListView中的item的格式在哪里约束呢?同理,为其中的item自然也需要相应的xml文件来进行配置,在layout文件加下新建一个xml文件,如下:
item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"