这篇文章,主要介绍Android中提供的一些基本UI控件的使用,包含:TextView、Button、EditText、ImageView、ProgressBar组件。
目录
Android里面提供了很多的UI控件,并且Android中的所有控件可以有两种创建方式:
TextView控件的主要作用是用于显示文本内容的,Android里面通过XML设置属性时候,所有的属性几乎都是使用【android:】开头。
下面看下TextView这个控件的属性:
- layout_width:设置控件宽度,三个可选值(fill_parent,match_parent,wrap_content),也可以直接使用数字,采用【dp】单位。
- layout_height:设置控件高度,三个可选值(fill_parent,match_parent,wrap_content),也可以直接使用数字,采用【dp】单位。
- id:控件的唯一标识。
- text:控件显示的内容。
- textColor:控件字体颜色。
- textStyle:控件字体的风格,三个可选值(normal正常,bold加粗,italic斜体)。
- textSize:控件字体大写。一般采用【sp】作为单位。
- background:控件的背景颜色,也可以是图片。
- gravity:控件中内容对齐方式。这个有很多的可选值,上下左右水平垂直等等方向,查询需要的属性即可。
- shadowColor:设置阴影颜色。
- shadowRadius:设置阴影的模糊程度,设为0.1就变成了字体颜色,建议使用3.0。
- shadowDx:设置阴影在水平方向的偏移。
- shadowDy:设置阴影在竖直方向的偏移。
下面看个案例代码:



- <TextView
- android:id="@+id/tv01"
- android:text="这是TextView的文本内容"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:textSize="100sp"
- android:textColor="#FFFF0000"
- android:background="@color/black"
- android:textStyle="bold"
- android:gravity="center"/>


- <TextView
- android:id="@+id/tv02"
- android:text="设置阴影"
- android:textSize="40sp"
- android:textStyle="bold"
- android:textColor="@color/black"
- android:shadowColor="#FFFF0000"
- android:shadowRadius="3.0"
- android:shadowDx="30"
- android:shadowDy="20"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- />

跑马灯效果是指:一行文字可以自动的移动,并且是循环。
- singleLine:内容单行显示。
- focusable:是否可以获取焦点。
- fosusableInTouchMode:触摸的时候是否可以获取焦点。
- ellipsize:省略文本(是否显示【...】的格式),要实现跑马灯效果就设置为【marquee】。
- marqueeRepeatLimit:字幕动画重复的次数。
- clickable:是否可点击。
- <TextView
- android:id="@+id/tv03"
- android:text="采用TextView控件实现跑马灯效果"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:ellipsize="marquee"
- android:focusable="true"
- android:marqueeRepeatLimit="marquee_forever"
- android:singleLine="true"
- android:clickable="true"
- android:focusableInTouchMode="true"
- android:textSize="40sp"/>
启动App,然后点击文字,就可以看到文字自己会从右往左滚动。

到这里,【TextView】控件常用的属性就介绍完了。
Button是按钮控件,它是一个继承自TextView控件的子控件。
- <Button
- android:id="@+id/btn01"
- android:text="这是按钮"
- android:layout_width="200dp"
- android:layout_height="100dp"/>

这里我们实现一个功能,就是点击按钮的时候,按钮背景会自动切换。通过【android:background】属性设置背景,但是这种不能直接设置颜色,要设置drawable类型,才能够生效。





- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
-
- <!-- 当点击时候显示这个drawable -->
- <item android:drawable="@drawable/ic_baseline_alarm_on_24" android:state_pressed="true"/>
- <!-- 默认显示的图标 -->
- <item android:drawable="@drawable/ic_baseline_bug_report_24"/>
-
- </selector>
设置按钮的背景为drawable的XML文件。
- <Button
- android:id="@+id/btn02"
- android:text="这是按钮02"
- android:background="@drawable/btn_selector"
- android:layout_width="200dp"
- android:layout_height="100dp"/>


Button控件有三个事件:点击事件,长按事件,触摸事件。事件需要通过事件监听器来获取,三个监听器分别如下所示:
- setOnClickListener:点击事件监听器。重写【onClick】方法。
- setOnLongClickListener:长按事件监听器。重写【onLongClick】方法。
- setOnTouchListener:触摸事件监听器。重写【onTouch】方法。
看下案例代码:
- package com.android.demo;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.os.Bundle;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.Button;
-
- public class MainActivity extends AppCompatActivity {
-
- private static final String TAG = "btn";
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // setContentView(R.layout.activity_main);
- // setContentView(R.layout.activity_textview);
- setContentView(R.layout.activity_button);
-
- // 获取按钮
- Button btn01 = findViewById(R.id.btn01);
- // 设置监听事件
- btn01.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Log.i(TAG, "触发按钮的点击事件");
- }
- });
- btn01.setOnLongClickListener(new View.OnLongClickListener() {
- @Override
- public boolean onLongClick(View v) {
- Log.i(TAG, "触发按钮的长按事件");
- return false;
- }
- });
- btn01.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- Log.i(TAG, "触发按钮的触摸事件");
- return false;
- }
- });
- }
- }
启动应用,点击按钮,查看【Logcat】控制台,可以看到如下输出日志。

我们也可以在XML里面通过属性来设置触发的事件,然后属性值填写对应的方法名称,接着在对应的java类里面,实现对应的方法即可。

以上,就是Button控件的相关属性和事件。
EditText是一个文本输入框的控件,它也是继承自TextView控件的。它有如下属性:
- hint:输入提示,类似于HTML中的placeholder。
- textColorHint:输入提示文字的颜色。
- inputType:输入框类型,是数字,字符,密码框还是其他的。
- drawableXxxx:输入框前面的图标(Xxxx是指具体的图标名称)。
- drawablePadding:设置图标和输入框的间距。
- paddingXxxx:设置文本内容和输入框的间距(Xxxx是上下左右哪个间距)。
- background:输入框的背景颜色。
- <?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"
- android:orientation="vertical"
- tools:context=".edittext">
-
- <EditText
- android:id="@+id/edt01"
- android:hint="请输入用户名称"
- android:layout_width="200dp"
- android:layout_height="80dp"/>
-
- <EditText
- android:id="@+id/edt02"
- android:hint="请输入用户名称"
- android:inputType="numberPassword"
- android:layout_width="200dp"
- android:layout_height="80dp"/>
-
- <EditText
- android:id="@+id/edt03"
- android:hint="请输入用户名称"
- android:inputType="text"
- android:textColorHint="#95a1aa"
- android:layout_width="200dp"
- android:layout_height="80dp"/>
-
- <EditText
- android:id="@+id/edt04"
- android:hint="请输入用户名称"
- android:inputType="text"
- android:textColorHint="#95a1aa"
- android:drawableLeft="@drawable/ic_baseline_how_to_reg_24"
- android:drawablePadding="20dp"
- android:paddingLeft="10dp"
- android:layout_width="200dp"
- android:layout_height="80dp"/>
-
- </LinearLayout>
运行效果如下所示:

要获取输入框的内容,一般是结合Button按钮控件,当我们点击按钮的时候,通过触发按钮的点击事件,然后在方法里获取EditText输入框的内容。
- <Button
- android:id="@+id/btn01"
- android:text="获取内容"
- android:layout_width="200dp"
- android:layout_height="80dp"/>
- // 获取按钮
- Button btn01 = findViewById(R.id.btn01);
- // 获取输入框
- EditText editText = findViewById(R.id.edt04);
- // 设置点击事件
- btn01.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 获取输入框内容
- Editable text = editText.getText();
- Log.i(TAG, "输入框的内容是: " + text.toString());
- }
- });
运行应用,可以看到控制台的输出日志。

ImageView控件是用于存放图片的容器,它也是TextView的一个子类。ImageView控件具有如下属性:
- src:指定图片的路径。
- scaleType:设置图片的缩放类型。
- maxWidth:设置图片的最大宽度,当图片宽度达到最大值后,高度会等比缩放。
- maxHeight:设置图片的最大高度,当图片高度达到最大值后,宽度会等比缩放。
- adjustViewBounds:调整view容器的界限,是否自动调整图片的边界大小。
新建一个【activity】,然后在【drawable】目录下面,放一张测试图片,图片任意就可以啦。
- <?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"
- android:orientation="vertical"
- tools:context=".imageview">
-
- <ImageView
- android:id="@+id/img01"
- android:src="@drawable/ceshi"
- android:layout_width="200dp"
- android:layout_height="200dp"/>
-
- <ImageView
- android:id="@+id/img02"
- android:src="@drawable/ceshi"
- android:scaleType="centerCrop"
- android:layout_width="200dp"
- android:layout_height="200dp"/>
-
- <ImageView
- android:id="@+id/img03"
- android:src="@drawable/ceshi"
- android:maxWidth="200dp"
- android:maxHeight="200dp"
- android:adjustViewBounds="true"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
-
- </LinearLayout>
运行效果如下所示:

ProgressBar是一个进度条控件,它也是TextView的子类。具有如下属性:
- max:进度条最大值。
- progress:进度条已经完成的数值。
- indeterminate:是否精确显示进度条的数值,true表示不精确显示进度。
- style:进度条的样式(android里面提供了一些进度条样式,我们可以直接使用)。
- <?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"
- android:orientation="vertical"
- tools:context=".progressbar">
-
- <!-- 默认转圈圈的进度条 -->
- <ProgressBar
- android:id="@+id/pro01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
-
- <!-- 设置水平进度条 -->
- <ProgressBar
- android:id="@+id/pro02"
- style="?android:attr/progressBarStyleHorizontal"
- android:max="100"
- android:layout_width="200dp"
- android:layout_height="wrap_content"/>
- <!-- 创建按钮,点击按钮时候,进度条增加 -->
- <Button
- android:id="@+id/btn01"
- android:text="增加进度条"
- android:layout_width="120dp"
- android:layout_height="60dp"/>
-
- <!-- 设置水平进度条,不显示进度值 -->
- <ProgressBar
- android:id="@+id/pro03"
- style="?android:attr/progressBarStyleHorizontal"
- android:max="100"
- android:layout_width="200dp"
- android:indeterminate="true"
- android:layout_height="wrap_content"/>
-
- </LinearLayout>
java代码如下所示:
- // 获取按钮
- Button btn01 = findViewById(R.id.btn01);
- // 设置点击事件
- btn01.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 获取进度条,将进度条数值增加
- ProgressBar progress = findViewById(R.id.pro02);
- // 获取进度条数值
- int num = progress.getProgress();
- // 加10
- num += 10;
- // 重新设置进度条数值
- progress.setProgress(num);
- }
- });
查看效果如下所示:

以上,就是ProgressBar进度条组件的使用。
综上,这篇文章结束了,主要介绍了Android中提供的一些基本UI控件的使用,包含:TextView、Button、EditText、ImageView、ProgressBar组件。