首页 > 系统 > Android > 正文

Android_ListView的用法

2019-11-09 18:13:10
字体:
来源:转载
供稿:网友

ListView是一种非常常见的一个组件,以垂直列表的形式显示列表项。而完成一个简单的ListView只需要这么三个步骤:

1、在布局文件xml中添加ListView并配置相应的属性

2、在资源文件XML中添加列表项

3、将布局文件应用上

举例:

1、在布局文件中加ListView并配置相应属性,如下配置了宽度、高度、分割线高度、是否显示头部分割线、列表项

<?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="vertical">    <ListView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:dividerHeight="5px"        android:headerDividersEnabled="false"        android:entries="@array/list"/></LinearLayout>2、资源文件XML中添加列表项,新建XML文件,命名为arrays.xml,存放于Values文件夹中(开发环境为Android Studio)

<?xml version="1.0" encoding="utf-8"?><resources>    <string-array name="list">        <item>列表项1</item>        <item>列表项2</item>        <item>列表项3</item>        <item>列表项4</item>        <item>列表项5</item>        <item>列表项6</item>        <item>列表项7</item>    </string-array></resources> 3、在MainActivity中应用布局文件,即可显示列表项

这样就完成了一个最最简单的列表。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表