前言
这篇文章记录了作者学习Android Studio开发App的心得体会 1。Android Studio提供的最基础的控件是TextView
、ImageView
和Button
,通常TextView
用来显示文本,ImageView
用来显示图片,而Button
用来响应用户的点击。但是TextView
、ImageView
同样有setOnClickListener()
方法,所以可以用TextView
、ImageView
代替Button
,来实现响应用户点击的功能。
现在想要设计一个简单的响应用户点击的界面,从上到下分别是:
TextView
,宽度整个屏幕,高度50%,点击后随机增加聊天内容。ImageView
,宽度整个屏幕,高度50%,点击后有序改变图片内容。
一、布局文件的编写
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="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline_1"
app:layout_constraintGuide_percent=".50"
android:orientation="horizontal"/>
<TextView
android:id="@+id/tv_bbs"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text