Day 21 - Android Studio ImageView的基本使用

Day 21 - Android Studio ImageView的基本使用
昨天我们讲到了EditText的使用,讲到目前我们已经有很多种工具可以用了,但为了让我们有更多工具可以使用,我们今天还是要继续介绍,今天我们要介绍的就是ImageView的基本用法。

ImageView是用来做什麽的和如何使用?

在我们使用Android程序的时候,有时会看到程序中显示一些图片,这就是因为我们程序中有加入了ImageView,所以程序中才会有图片,我们就直接开始讲吧。

先从Common新增ImageView到我们的Design里,他会要求你选一张图片,我们这边就选左上角的加号,并且按Import Drawables,我就从电脑中随便选择一张图片
https://ithelp.ithome.com.tw/upload/images/20210921/201404191H2cyio4rw.png

接着我们来看xml的部分,src就是图片的来源,scaleType则可以设定我们图片的填补方式
visibility可以设定我们图片的可见或不可见

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="193dp"
    android:scaleType="fitCenter"
    android:visibility="visible"
    android:contentDescription="@string/todo"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/_42495512_887481111873490_5335864624436824629_n" />

介绍完了大概,我们这边就写一个程序,当我们按下按钮时,就会改变我们的图片是否可见
首先我们先import下列这些套件:

import android.view.View
import android.widget.ImageView
import androidx.core.view.isVisible

新增一个按钮到我们的Design,并写一个函式绑到按钮上
函式:

    fun press(view: View){
        val imageView:ImageView = findViewById(R.id.imageView2)
        if (imageView.isVisible){
            imageView.isVisible = false
        }else
            imageView.isVisible = true
    }

我们就直接运行看看吧!
程序运行的很成功!
https://ithelp.ithome.com.tw/upload/images/20210921/20140419EDeKgpfQNK.png
https://ithelp.ithome.com.tw/upload/images/20210921/201404199NdSPDk9IZ.png


<<:  【领域展开 06 式】 WordPress 主机商与网域选购

>>:  Leetcode: 80. Remove Duplicates from Sorted Array II

有关版本控制

在开发的过程一定会面临各式的测试,以及功能的增减。对於文件的编写也会有相同的要求。一般来说,个人开发...

DAY16:清单元件之简介

今天要来介绍的是清单列表,像是我们平常打开IG或FB的通知栏时或是通讯软件的聊天室列表跟好友列表就都...

[Day-3] R语言 - 分群分类 傻傻分不清楚~ (clustering vs classification)

您的订阅是我制作影片的动力 订阅点这里~ 若内容有误,还请留言指正,谢谢您的指教 ...

【Day29】修改 Proxy 的方法

今天要来分享之前实习开发 QA Bot, LUIS Bot 设定 proxy 的地方。 QnA Ma...

【Day4】 环境建置 - 安装 VisualStudio Code on Mac

前言 最近刚好换了 macbook pro m1,可以趁着这个机会顺便学习如何在Mac上安装编译器。...