Android ListView UI组件使用说明
作者:心悦君兮君不知-睿 时间:2022-06-14 16:04:09
一、ListView
该组件是android中最常用的一个UI组件,用于实现在屏幕上显示多个内容,以便于我们用手指来回翻转。
先在layout中进行布局我们的组件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
对该组件注册一个list_view的ID(这个R中的语句是运行时会自动生成的),可在这里看到
这样这个组件就定义好了,然后在活动的源码中进行注册
package com.example.listviewtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
//import java.lang.ArrayAdapter;
public class MainActivity extends Activity {
private String[] data = {"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this,android.R.layout.simple_list_item_1,data);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
}
可以看出这里使用了一个Android自带适配器类ArrayAdapter,使用泛型String的实例创建,然后传入参数,分别为上下文实例,android自带的一个list_item_1的内部布局文件,里面只有一个TextView,可用于显示一段简单的文本;最后一个参数就是我们传入的数据。
创建一个ListView的实例,并且找到这个R文件的listView地址。最后调用setAdapter()方法,即为设置完毕。
二、源码:
项目地址
https://github.com/ruigege66/Android/tree/master/ListViewTest
来源:https://www.cnblogs.com/ruigege0000/p/12630025.html
标签:Android,ListView,组件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android如何实现APP自动更新
2021-10-31 06:09:27
![](https://img.aspxhome.com/file/2023/6/116156_0s.gif)
5分钟用C#实现串口助手
2022-01-20 09:43:00
![](https://img.aspxhome.com/file/2023/4/125984_0s.png)
利用adt-bundle轻松搭建Android开发环境与Hello world(Windows)
2022-09-05 14:35:18
![](https://img.aspxhome.com/file/2023/0/108310_0s.png)
Java如何实现字符串每隔4位加空格
2023-11-27 06:00:09
Kotlin 基础教程之数组容器
2021-09-09 17:22:40
C# wpf Grid中实现控件拖动调整大小的示例代码
2023-05-15 17:12:03
![](https://img.aspxhome.com/file/2023/0/94650_0s.png)
Android自定义Banner轮播效果
2023-08-05 23:34:06
浅谈MyBatis 事务管理
2022-03-22 16:17:11
Java内存模型之happens-before概念详解
2023-11-23 03:11:50
![](https://img.aspxhome.com/file/2023/1/59971_0s.png)
Springcloud seata nacos环境搭建过程图解
2022-11-15 00:34:14
![](https://img.aspxhome.com/file/2023/1/61341_0s.png)
java两个integer数据判断相等用==还是equals
2021-06-14 00:46:52
Unity实现弹球打砖块游戏
2021-09-24 16:13:08
![](https://img.aspxhome.com/file/2023/9/104349_0s.jpg)
Android第三方开源下拉框NiceSpinner使用详解
2023-01-04 11:10:29
![](https://img.aspxhome.com/file/2023/7/138837_0s.gif)
MyBatis中resultType和parameterType和resultMap使用总结
2023-04-01 05:43:42
c#解压文件的实例方法
2022-04-25 09:49:40
Springboot 整合 RocketMQ 收发消息的配置过程
2023-01-22 22:49:28
如何将javaweb项目部署到linux下
2023-11-11 11:45:11
IDEA实现JDBC的操作步骤
2021-08-29 11:08:55
![](https://img.aspxhome.com/file/2023/8/83428_0s.jpg)
C++时间戳转换成日期时间的步骤和示例代码
2021-05-27 18:50:07
Java中为什么start方法不能重复调用而run方法可以?
2023-11-15 03:04:02
![](https://img.aspxhome.com/file/2023/8/59598_0s.webp)