我是靠谱客的博主 乐观皮皮虾,这篇文章主要介绍Android 列表视图制作古诗词,现在分享给大家,希望可以做个参考。

文档目录

    • 一、案例操作
      • 1、创建安卓应用【ReadAncientPoetry】
      • 2、将背景图片拷贝到drawable目录
      • 3、布局资源文件activity_main.xml
      • 4、诗歌列表项模板poem_list_item.xml
      • 5、字符串资源文件strings.xml
      • 6、主界面类 - MainActivity
      • 7、启动应用,查看效果
      • 8、单击列表项,弹出吐司,显示古诗名及编号
      • 9、启动应用,查看效果
      • 10、创建显示古诗内容的界面 - ContentActivity
      • 11、内容界面布局资源文件content_activity.xml
      • 12、字符串资源文件strings.xml
      • 13、修改主界面类,实现窗口跳转
      • 14、修改内容界面 - ContentActivity

一、案例操作

1、创建安卓应用【ReadAncientPoetry】

在这里插入图片描述
在这里插入图片描述

2、将背景图片拷贝到drawable目录

在这里插入图片描述

3、布局资源文件activity_main.xml

在这里插入图片描述

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?> <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:background="@drawable/background" android:padding="15dp" tools:context=".MainActivity"> <ListView android:id="@+id/lvPoemTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="#aaaaaa" android:dividerHeight="0.5dp"/> </LinearLayout>

4、诗歌列表项模板poem_list_item.xml

在这里插入图片描述

复制代码
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tvPoemTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:minHeight="80dp" android:textColor="#0000ff" android:textSize="25sp" />

5、字符串资源文件strings.xml

在这里插入图片描述

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<resources> <string name="app_name">阅读古诗</string> <string-array name="titles"> <item>静夜思</item> <item>登鹳雀楼</item> <item>相思</item> <item>听弹琴</item> <item>登乐游原</item> <item>朝发白帝城</item> <item>巴山夜雨</item> <item>渭城曲</item> <item>春夜喜雨</item> <item>离离原上草</item> <item>示儿</item> </string-array> <string-array name="authors"> <item>唐·李白</item> <item>唐·王之涣</item> <item>唐·王维</item> <item>唐·刘长卿</item> <item>唐·李商隐</item> <item>唐·李白</item> <item>唐·杜牧</item> <item>唐·王维</item> <item>唐·杜甫</item> <item>唐·白居易</item> <item>宋·陆游</item> </string-array> <string-array name="contents"> <item>床前明月光,n疑是地上霜。n举头望明月,n低头思故乡。</item> <item>白日依山尽,n黄河入海流。n欲穷千里目,n更上一层楼。</item> <item>红豆生南国,n春来发几枝。n愿君多采撷,n此物最相思。</item> <item>泠泠七弦上,n静听松风寒。n古调虽自爱,n今人多不弹。</item> <item>向晚意不适,n驱车登古原。n夕阳无限好,n只是近黄昏。</item> <item>朝辞白帝彩云间,n千里江陵一日还。n两岸猿声啼不住,n轻舟已过万重山。</item> <item>君问归期未有期,n巴山夜雨涨秋池。n何当共剪西窗烛,n却话巴山夜雨时。</item> <item>渭城朝雨浥轻尘,n客舍青青柳色新。n劝君更尽一杯酒,n西出阳关无故人。</item> <item>好雨知时节,当春乃发生。n随风潜入夜,润物细无声。n野径云俱黑,江船火独明。n晓看红湿处,花重锦官城。</item> <item>离离原上草,一岁一枯荣。n野火烧不尽,春风吹又生。n远芳侵古道,晴翠接荒城。n又送王孙去,萋萋满别情。</item> <item>死去元知万事空,n但悲不见九州同。n王师北定中原日,n家祭无忘告乃翁。 </item> </string-array> </resources>

6、主界面类 - MainActivity

在这里插入图片描述

  • 声明变量
    在这里插入图片描述

  • 通过资源标识符获取控件实例
    在这里插入图片描述

  • 初始化古诗标题数组
    在这里插入图片描述

  • 创建数组适配器
    在这里插入图片描述

  • 给列表控件设置适配器
    在这里插入图片描述

7、启动应用,查看效果

  • 这是采用平台提供的列表项模板的效果
    在这里插入图片描述
  • 下面我们采用自定义的列表项模板poem_list_item.xml
    在这里插入图片描述
  • 滑动列表控件效果演示请添加图片描述

8、单击列表项,弹出吐司,显示古诗名及编号

  • 修改主界面类,给列表控件注册项目单击监听器
    在这里插入图片描述
  • 参数position是用户单击列表项的位置,从0开始的,比如用户单击了第3个列表项,那么position就等于2,也就是说,position要加1才是行号
  • 参数id的值与参数position的值相等,两个参数只是类型不同而已

9、启动应用,查看效果

请添加图片描述

10、创建显示古诗内容的界面 - ContentActivity

  • 基于模板来创建ContentActivity
    在这里插入图片描述
    在这里插入图片描述

11、内容界面布局资源文件content_activity.xml

在这里插入图片描述

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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:background="@drawable/background" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_marginTop="30dp" android:layout_marginBottom="20dp" android:layout_weight="1" android:gravity="center_vertical" android:textColor="#ff00ff" android:textSize="40sp" /> <TextView android:id="@+id/tvAuthor" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_marginBottom="20dp" android:layout_weight="1" android:gravity="center_vertical" android:textColor="#000000" android:textSize="25sp" /> <TextView android:id="@+id/tvContent" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_marginBottom="20dp" android:layout_weight="8" android:textColor="#0000ff" android:textSize="30sp" /> <Button android:id="@+id/btnBack" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:onClick="doBack" android:text="@string/back" android:textSize="20sp" /> </LinearLayout>

12、字符串资源文件strings.xml

在这里插入图片描述

13、修改主界面类,实现窗口跳转

在这里插入图片描述

14、修改内容界面 - ContentActivity

在这里插入图片描述

  • 声明变量
    在这里插入图片描述
  • 通过资源标识符获取控件实例
    在这里插入图片描述
  • 获取窗口跳转的意图,获取意图携带的数据,进行相应的处理
    在这里插入图片描述
  • 返回目录按钮单击事件处理方法
    在这里插入图片描述
    15、启动应用,查看效果

请添加图片描述

最后

以上就是乐观皮皮虾最近收集整理的关于Android 列表视图制作古诗词的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部