ListView简介

ListView是android中常用的一种控件,创建ListView有两种方式:

1.在xml中使用ListView控件创建。

2.使用activity继承ListActivity,然后使用setListAdapter()创建,如果需要在xml中创建,需要android:id = @id:android:list

Listview和GridView和Spinner和Gallery等Adapterview都是容器,用adapter来提供数据,而adapterView负责数据的显示形式。

AdapterView属性讲解

AbsListView的属性:



ListView提供的属性

在xml设置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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">
<ListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:entries="@array/books"
    android:divider="#f00"
    android:dividerHeight="2px"
    android:headerDividersEnabled="false"
    />
</LinearLayout>
<?xml version="1.0" encoding="UTF-8"?>
    <resources>
        <string-array name="books">
            <item>hello</item>
            <item>world</item>
            <item>liu</item>
            <item>peng</item>
        </string-array>
    </resources>

使用ArrayAdapter创建ListView

package peng.liu.testview;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.ListView;

public class MainActivity extends Activity{
    private String attrs[] = {"hello","world","liu","peng'"};
    private String names[] = {"java","c","python"};
    private ListView list1,list2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list1 = (ListView) findViewById(R.id.list1);
        list2 = (ListView) findViewById(R.id.list2);
        ArrayAdapter<String> array1 = new ArrayAdapter(this,android.R.layout.simple_list_item_1,attrs);
        ArrayAdapter<String> array2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names);
        list1.setAdapter(array1);
        list2.setAdapter(array2);
    }
}
<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list1"
        android:dividerHeight="2px"
        android:divider="#f00"
        android:headerDividersEnabled="false"
        />
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list2"
        android:dividerHeight="2px"
        android:divider="#f00"
        android:headerDividersEnabled="false"
        />
</LinearLayout>

基于ListActivity创建

package peng.liu.testview;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity {
    private String attrs[] = {"hello","world","liu","peng'"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayAdapter<String> array1 = new ArrayAdapter(this,android.R.layout.simple_list_item_1,attrs);
        setListAdapter(array1);
    }

}

Android列表视图ListView和ListActivity-android学习之旅(二十四)的更多相关文章

  1. Android 自学之列表视图ListView和ListActivity

    ListView是手机系统中使用非常广泛的一种组件,它以垂直列表的形式显示所有列表项. 创建ListView有两种方式: 直接使用ListView创建. 让Activity继承ListActivity ...

  2. Android——列表视图(ListView)

    列表视图是android中最常用的一种视图组件,它以垂直列表的形式列出需要显示的列表项.在android中有两种方法向屏幕中添加列表视图:一种是直接使用ListView组件创建:另外一种是让Activ ...

  3. Android——列表视图 ListView(三)BaseAdapter

    activity_activitybase.xml <?xml version="1.0" encoding="utf-8"?> <ListV ...

  4. Android——列表视图 ListView(二)SimpleAdapter

    SimpleAdapter:可显示文字加图片 activity_activitysimple.xml <?xml version="1.0" encoding="u ...

  5. Android——列表视图 ListView(一)Arrayadapter

    一.ArrayAdapter 只显示文字 activitylistview_layout.xml <?xml version="1.0" encoding="utf ...

  6. 列表视图(ListView和ListActivity)

    在ListView中显示网络图片 ImageView 类虽然有一个 setImageUri 方法,但不能直接接受一个由网络地址生成的uri作为参数从而显示图片,我们只好使用其 setImageBitm ...

  7. Android学习路线(二十四)ActionBar Fragment运用最佳实践

    转载请注明出处:http://blog.csdn.net/sweetvvck/article/details/38645297 通过前面的几篇博客.大家看到了Google是怎样解释action bar ...

  8. Android 自定义View-android学习之旅(十四)

    自定义View的步骤 当andoid提供的系统组件不满足要求时候,完全可以集成View来派生自定义组件. 首定定义一个继承View的子类,然后重写他一个或几个方法. 重写的方法介绍 构造器:这是定制V ...

  9. Dynamic CRM 2013学习笔记(二十四)页面保存前进行逻辑验证

    我们有时要验证下页面上的一些逻辑,比如开始时间不能晚于结束时间,不对时不让保存.我们可以在相关的字段事件上处理,但这如果要判断的字段比较多时,就比较麻烦了. 这时候我们就可以利用Form的OnSave ...

随机推荐

  1. Mysq 索引优化

    MYSQL支持的索引类型 BTREE索引 特点: 通过引用以B+权的结构存储数据 能够加快数据的查询速度 更适合进行范围查找 应用: 全值匹配的查询 = 匹配最左前缀的查询 匹配列前缀查询 LIKE ...

  2. Unix系统的文件打开机构

    当打开一个文件时,建立用户与该文件的联系.其实质是将该文件在辅存中的有关目录信息.辅存i节点及相应的文件地址索引表拷贝到主存中.文件系统中管理这一方面的机构成为打开文件管理机构,简称打开文件机构. 打 ...

  3. C语言程序设计预报作业

    1. 阅读邹欣老师的博客--师生关系,针对文中的几种师生关系谈谈你的看法,你期望的师生关系是什么样的? 答:万物都是变化的,师生关系也一样.小学中学把老师看作春蚕,蜡烛的的比喻到了大学显然行不通了.大 ...

  4. A TensorBoard plugin for visualizing arbitrary tensors in a video as your network trains.Beholder是一个TensorBoard插件,用于在模型训练时查看视频帧。

    Beholder is a TensorBoard plugin for viewing frames of a video while your model trains. It comes wit ...

  5. windows server 2008 R2 禁用ipv6和隧道适配器

    在windows server 2008 R2操作系统下部署weblogic web application,部署完成后进行测试,发现测试页的地址使用的是隧道适配器的地址,而不是静态的ip地址,而且所 ...

  6. BI之SSIS入门最新版Visual Studio调试技巧

    简介 最近公司业务需要用到BI SSIS,SSIS是什么?"SSIS是Microsoft SQL Server Integration Services的简称,是生成高性能数据集成解决方案( ...

  7. Java网络爬虫Hello world实现——Httpclient爬取百度首页

    1.创建Maven项目 2.Httpclient Maven地址 <dependency> <groupId>org.apache.httpcomponents</gro ...

  8. centos6.8下weblogic12c静默安装

    环境: centos6.8 无桌面环境 jdk1.7.0_25 关闭iptables.selinux 安装前准备: 1.新建weblogic用户,设置weblogic密码 useradd weblog ...

  9. xshell连接centos与ubuntu

    操作系统:Windows 7 应用软件:Ware Workstation &Xshell 5 Linux:CentOS 7 Minimal &Ubuntu Server 16 ==== ...

  10. 防止SpringMVC拦截器拦截js等静态资源文件

    SpringMVC提供<mvc:resources>来设置静态资源,但是增加该设置如果采用通配符的方式增加拦截器的话仍然会被拦截器拦截,可采用如下方案进行解决: 方案一.拦截器中增加针对静 ...