参考《疯狂android讲义》第2.5节P94

1、创建一个或者多个ListView

<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" > <ListView
android:id="@+id/list1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#f00"
android:dividerHeight="2dp"
android:headerDividersEnabled="false" /> <ListView
android:id="@+id/list2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#0f0"
android:dividerHeight="2dp"
android:headerDividersEnabled="true" />
</LinearLayout>

2、创建TextViewResource,为每个元素定义其显示属性

array_item1.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_item1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:padding="10dp"
android:shadowColor="#0f0"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="2">
</TextView>

array_item2.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_item2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:padding="10dp"
android:shadowColor="#ff0"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="2">
</TextView>

3、创建Activity,并完成以下三个步骤

package com.ljh.listviewdemo;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //3、定义每个元素的内容
String[] arr1 = new String[]{"孙悟空","猪八戒","沙僧","唐僧"}; //4、将元素属性及元素内容包装为ArrayAdapter
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.array_item1, arr1); //5、为ListView设置Adapter
ListView list1 = (ListView) findViewById(R.id.list1);
list1.setAdapter(adapter1); //3、定义每个元素的内容
String[] arr2 = new String[]{"Java","C++","Python","PHP"}; //4、将元素属性及元素内容包装为ArrayAdapter
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.array_item2, arr2); //5、为ListView设置Adapter
ListView list2= (ListView) findViewById(R.id.list2);
list2.setAdapter(adapter2);
} }

创建ListView的基本步骤的更多相关文章

  1. 创建ListView的基本步骤 分类: H1_ANDROID 2013-10-31 23:25 1276人阅读 评论(0) 收藏

    参考<疯狂android讲义>第2.5节P94 1.创建一个或者多个ListView <LinearLayout xmlns:android="http://schemas ...

  2. Android开发10.1:UI组件适配器AdapterView(创建ListView,Adapter接口)

    @version:Android4.3 API18 @author:liuxinming 概述               AdapterView继承了ViewGroup,它的本质是容器       ...

  3. 别以为真懂Openstack: 虚拟机创建的50个步骤和100个知识点(5)

    八.KVM 这一步,像virsh start命令一样,将虚拟机启动起来了.虚拟机启动之后,还有很多的步骤需要完成. 步骤38:从DHCP Server获取IP 有时候往往数据库里面,VM已经有了IP, ...

  4. 别以为真懂Openstack: 虚拟机创建的50个步骤和100个知识点(3)

    四.Nova-compute 步骤17:nova-compute接收到请求后,通过Resource Tracker将创建虚拟机所需要的资源声明占用 步骤18:调用Neutron API配置Networ ...

  5. 创建CrawlSpider爬虫简要步骤

    创建CrawlSpider爬虫简要步骤: 1. 创建项目文件: e.g: scrapy startproject douyu (douyu为项目名自定义) 2. 进入项目文件: e.g: cd dou ...

  6. 【HANA系列】SAP ECLIPSE中创建ABAP项目的步骤

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP ECLIPSE中创建AB ...

  7. Linux下Weblogic创建域方法和步骤

    Weblogic 创建域 以weblogic帐号登录(与创建域目录相对应账户) cd /home/weblogic/bea/weblogic92/common/bin 执行./config.sh进入配 ...

  8. 创建ListView控件

    // 创建List控件 HWND hListView = CreateWindow(WC_LISTVIEW ,/*listview 宏的名字*/ L"" ,/*窗口标题*/ WS_ ...

  9. 使用SimpleAdapter创建ListView

    通过ArrayAdapter实现Adapter虽然简单.易用,但ArrayAdapter的功能比较有限.它的每个列表只能是TextView.如果开发者需呀实现更复杂的列表项,则可以考虑使用Simple ...

随机推荐

  1. 【android】android调用模拟器超时问题

    问题如下: 解决方案: 1)重启连接桥 C:\Users\hacket>adb kill-server C:\Users\hacket>adb start-server或者 adb log ...

  2. thinkPHP中省市级联下拉列表

    公共函数放置位置common文件夹下common.php文件(此段代码也可放置在要使用的控制器中) 封装的下拉列表函数代码: /** * 根据列表拼装成一个下拉列表 ADD BY CK * @para ...

  3. Lonely Integer

    https://www.hackerrank.com/challenges/lonely-integer def main(): n = int(raw_input()) s = dict() a = ...

  4. Android开源框架之SwipeListView导入及模拟QQ侧滑

    SwipeListView是Github上的一个开源框架,地址:https://github.com/47deg/android-swipelistview SwipeListView was bor ...

  5. Keil C51怎样将子程序段定位在固定的地址位?

    以下2问题均要用C51解决1.怎样将1个子程序段定位在1个固定的地址位置?例如将 INT BCD2HEX(INT XX)定位在1000H2.如何在EEPROM 中固定的位置存放1字符串?如在200H处 ...

  6. gnuplot

    一. 简介 gnuplot是一个命令行驱动的科学绘图工具,可将数学函数或数值资料以平面图或立体图的形式画在不同种类终端机或绘图输出装置上.gnuplot既支持命令行交互模式,也支持脚本. 二. 特性 ...

  7. Digit Stack

    Digit Stack In computer science, a stack is a particular kind of data type or collection in which th ...

  8. JS代码的window.location属性详解

    转载:http://www.5icool.org/a/201105/a564.html 如果你稍微懂一些JS代码,一般都会知道 window.location.href 这个属性.并且用该属性获取页面 ...

  9. c# list exists(contains) delegate 委托判断 元素是否在LIST中存在

    static void Main(string[] args)        {            List<GoodsInfo> list = new List<GoodsIn ...

  10. qt model/view 架构基础介绍之QTableWidget

    # -*- coding: utf-8 -*- # python:2.x #说明:QTreeWidget用于展示树型结构,也就是层次结构同前面说的 QListWidget 类似,这个类需要同另外一个辅 ...