《Android Studio开发实战》学习(八)- 点击按钮切换图片
背景
在前一篇文章 1中实现了使用Android Studio开发一个图片展示App,熟悉了简单控件ImageView的使用 2,在这里继续研究Android Studio的使用方法。本文的目的是介绍如何开发一个图片切换App,实现点击按钮,在窗口中切换不同的图片。
问题描述
现在想要设计一个图片切换工具,页面布局是
- 图片显示窗口,宽度是70%,高度100%,用来展示图片;
- 按钮面板,宽度是30%,高度为100%,包括1个按钮,点击按钮在窗口依次展示上传的图片。
将图片添加到Android Studio资源中
将图片添加到Android Studio资源中的方法很简单 3,只需要将图片拷贝到AndroidStudioProjects文件夹下当前工程的drawable文件夹内,在Android Studio界面的资源列表中就能看到,可以这样
cp image1.jpg ~/AndroidStudioProjects/ClickChangeImage/app/src/main/res/drawable
cp image2.jpg ~/AndroidStudioProjects/ClickChangeImage/app/src/main/res/drawable
cp image3.jpg ~/AndroidStudioProjects/ClickChangeImage/app/src/main/res/drawable
然后在资源列表中打开res/drawable目录,就能看到刚才添加的图片(如果没有显示,右击drawable文件夹,然后Reload from Disk)。注意目前Android Studio还不支持中文名称的图片,图片的名称只能是小写字母a-z,数字0-9和下划线(不能包括点.
,否则识别不了),如图所示。
关闭APP中标题的显示
想要在生成的应用APP中关闭当前工程的标题 4,可以把res/values/themes/themes.xml
中style
的parent
属性设置为parent="Theme.*.NoActionBar"
。
APP横屏显示
这样生成的app默认在手机上是纵向显示,如果想要横屏显示,则需要编辑manifests/AndroidManifest.xml
文件,在<activity>
标签中加一个属性android:screenOrientation="landscape"
5:
图片展示工具布局文件的编写
activity_main.xml
的代码如下所示:
<?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">
<androidx.constraintlayout.widget.Guideline
android:layout_width