ListView列表视图与Spinner
一、ListView
(一)基础知识
1.在Android开发中,ListView是比较常用的控件,它以列表的形式显示具体内容,并且能够根据数据的长度自适应显示。
e.g. 微信通信录(一行一行的、自适应——内容多的时候可以上下滑动)
2.在XML布局文件添加ListView的基本格局如下:
<ListView
属性列表
>
</ListView>
3.XML属性:
语句 | 属性 |
---|---|
android: driver | 用于为列表设置分隔条(可以用颜色,也可以用drawale资源分割) |
android: driverHeight | 用于设置分割条的高度 |
android: entries | 用于通过数组资源为listview制定列表项(制定每行显示的具体内容) |
(二)完成基础ListView
修改APP名称——
在该文件中写ListView中需要展现的内容。
实例
在strings.xml 中
<resources>
<string name="app_name">My Application</string>
<string-array name="item">
<item>我的设备</item>
<item>双卡和移动网络</item>
<item>WLAN</item>
<item>蓝牙</item>
<item>个人热点</item>
<item>更多连接方式</item>
<item>显示</item>
<item>壁纸</item>
<item>个性主题</item>
<item>声音与震动</item>
<item>锁屏、密码和指纹</item>
<item>通知和状态栏</item>
<item>桌面与最近人物</item>
</string-array>
</resources>
然后在activity_main.xml中引用:
android:entries="@array/item"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:entries="@array/item"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
(三)更改分隔线为自定义图片(自定义颜色)
在 ListView 中加入——
android:divider="@drawable/fengexian"
android:dividerHeight=“3dp”
即——
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:divider="@drawable/fengexian"
android:dividerHeight="3dp"
android:entries="@array/item"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
(但很奇怪,运行之后一直没有显示分隔线的颜色,甚至连分隔线都看不到了,我的天!)
二、Spinner
Android中提供的Spinner列表选择框相当于在网页中常见的下拉列表框,通常用于提供一系列可选择的列表项,供用户进行选择,从而方便用户。
在XML布局文件中,定义列表选择框可以使用==《Spinner》==标记,
基本的语法格式如下:
<Spinner
android:entries="@array/数组名称
android:layput_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/ID号"
>
</Spinner>
步骤
1.添加选项——
<resources>
<string name="app_name">My Application</string>
<string-array name="item">
<item>Java</item>
<item>C/C++</item>
<item>Python</item>
<item>Go</item>
<item>其他</item>
</string-array>
</resources>
2.引用——
android:entries="@array/item"
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:entries="@array/item"
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.28"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.246" />
</androidx.constraintlayout.widget.ConstraintLayout>
3.运行——