TextView控件
需要显示文本时可使用文本控件TextView实现功能。文本控件属性有设置文本内容、尺寸以及颜色等。可点击官方参考链接查看所有属性。
设置文本内容
设置文本内容一般有两种方式,一种是通过XML文件属性android:text
设置文本,如下:
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好,世界" />
另一种可以通过视图对象的setText
方法设置文本,如下:
//获取名为tv_hello的文本视图
TextView tv_hello = findViewById(R.id.tv_hello);
//设置tv_text内容
tv_hello.setText("你好,世界");
但为了修改方便,一般我们都在/res/values/strings.xml
下定义个字符串常量,如下:
<resources>
<string name="hello">你好,世界</string>
</resources>
当我们定义好字符串常量后,xml文件属性android:text
就可以写为如下形式:
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />
View视图方法setText
则可以写为如下形式:
//获取名为tv_hello的文本视图
TextView tv_hello = findViewById(R.id.tv_hello);
//设置tv_text内容
tv_hello.setText(R.string.hello);
设置文本大小
ViewText控件可通过xml文件设置文本大小,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_px"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="你好,世界(px大小)"
android:textSize="30px"
/>
<TextView
android:id="@+id/tv_dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好,世界(dp大小)"
android:textSize="30dp"
/>
<TextView
android:id="@+id/tv_sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你好,世界(sp大小)"
android:textSize="30sp"
/>
</LinearLayout>
也可以通过代码设置,如下:
//从布局中获取名为tv_sp的文本视图
TextView tv_sp = findViewById(R.id.tv_sp);
tv_sp.setTextSize(30); //设置tv_sp的文本大小
常用字号单位
在设置字体大小时,需要注意其单位,一般使用sp。下面介绍常用的几种单位:
px(像素)
这是屏幕上的基本点,代表屏幕的实际像素。例如,一个分辨率为1024×768像素的屏幕意味着横向有1024px,纵向有768px。不同设备的显示效果在这种单位下通常是相同的。
dpi
“dots per inch” 的缩写,“每英寸的像素数”,即像素密度。
dp(或dip,设备独立像素)
dp 全称是 “density- independent pixel”,即“密度无关像素”,所以也可以缩写成 dip,不过 dp 更常用。
在每英寸160点的显示器上,1dp等于1px。但随着屏幕密度的变化,dp和px之间的比例也会相应调整。定义dp为 160dpi 时的一个像素大小;那么到 320 dpi 时,它就相当于两个像素。
px、dp以及dpi三者的数值关系如下:
px= dp ×(dpi/160)
这样的话,假如做一个50×50dp的icon,放到160dpi的屏幕上就是50px×50px,放到320dpi的屏幕上就是100px×100px,各元素的布局和比例便不会失调,相当于当图片显示在高分辨率设备上时,图片分辨率也被放大了。
sp
这是基于dp的单位,但主要用于字体大小的表示。它考虑了用户的字体大小首选项,因此可以确保在不同设置下字体大小的一致性。例如,如果用户将系统字体大小设置为“大”,那么使用sp单位的文本也会相应地放大,因此字体的单位一般都用sp。
设置TextView颜色
无论是在xml文件中还是通过接口都可以设置TextView颜色,但在不同场景设置颜色需要注意不同事项。
在xml中设置文本颜色
以值的形式设置颜色
当以值的形式设置颜色时,不能用0x开头,只能使用#开头,如下设置android:textColor
属性:
<TextView
android:id="@+id/tv_xml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布局文件设置六位文字颜色"
android:textColor="#00ff00"
android:textSize="17sp" />
如上方代码设置颜色使用24bit颜色值时,则不透明度默认0xFF代表完全不透明。设置透明度需要自定义第24bit至31bit的值。
以宏定义的形式设置颜色
我们可以在res/values/colors.xml
文件中定义颜色值,如下定义color/green
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="green">#00ff00</color>
</resources>
再在布局xml文件中使用,如下设置android:textColor
属性:
<TextView
android:id="@+id/tv_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="资源文件引用六位文字颜色"
android:textColor="@color/green"
android:textSize="17sp" />
通过接口设置文本颜色
通过setTextColor
接口设置文本颜色,传入的参数为int值。可以使用Color类已定义的颜色值,也可以使用自己设定的值,但未指定不透明度时,默认的不透明度为0x00(全透明)。使用实例如下所示:
//从布局文件中获取名为tv_code_system的文本视图
TextView tv_code_system = findViewById(R.id.tv_code_system);
//将tv_code_system的文字颜色设置为系统自带的绿色
tv_code_system.setTextColor(Color.GREEN);
//从布局文件中获取名为tv_code_six的文本视图
TextView tv_code_six = findViewById(R.id.tv_code_six);
//设置颜色为透明绿色(透明状态下显示不出内容)
tv_code_six.setTextColor(0x00ff00);
//从布局文件中获取名为tv_code_eight的文本视图
TextView tv_code_eight = findViewById(R.id.tv_code_eight);
//将tv_code_eight设置为绿色
tv_code_eight.setTextColor(0xff00ff00);
设置背景色
当值以int型传参时需要使用setBackgroundColor
接口,代码如下:
//从布局文件中获取名为tv_code_background的文本视图
TextView tv_code_background = findViewById(R.id.tv_code_background);
// 将tv_code_background的背景颜色设置为绿色
tv_code_background.setBackgroundColor(Color.GREEN); //颜色来自代码
当使用res/values/colors.xml
定义的颜色值时,需要使用setBackgroundResource
接口,代码如下:
//从布局文件中获取名为tv_code_background的文本视图
TextView tv_code_background = findViewById(R.id.tv_code_background);
tv_code_background.setBackgroundResource(R.color.green); //颜色来自资源文件
当然android:background
属性不单单能设置背景色,还可以设置图片。
视图基础
视图的宽度和高度
安卓控件宽度使用android:layout_width
属性表示,高度使用android:layout_height
属性表示。宽高的取值主要有以下3种:
match_parent
表示与上级视图保持一致,上级尺寸多大,子级也多大。
wrap_content
表示内容自适应。以TextView为例,需要多大显示空间,空间就会多大。超过上级宽度时,文本换行;超过上级高度时,超过部分文本隐藏。
具体尺寸(单位:dp)
当宽度或高度设置为具体值时,表示宽度或高度就是这么大尺寸。
以上介绍的都是在xml布局文件中设置宽度和高,在代码中设置开度和高度需要先获取控件的宽高-》修改控件宽高-》设置宽高。值得注意的是,代码设置宽高单位都是px(像素),因此需要通过下列代码转换dp与px:
//根据手机的分辨率从dp的单位转成px(像素)
public static int dip2px(@NonNull Context context, float dpValue){
//获取当前手机的像素密度(1个dp对应几个px)
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue*scale + 0.5f);
}
经过单位转换后参照如下设置宽高:
//获取名为tv_code的文本视图
TextView tv_code = findViewById(R.id.tv_code);
//获取tv_code的布局参数(含宽度和高度)
ViewGroup.LayoutParams params = tv_code.getLayoutParams();
//修改布局参数中的宽度数值,注意默认是px单位,需要把dp数值转换成px数值
params.width = Utils.dip2px(this, 300);
//设置tv_code的布局参数
tv_code.setLayoutParams(params);
视图的间距
间距有外部间距(android:layout_margin)和内部间距(android:padding)两种。两种间距可单独设置上、下、左、右四个边距,外部边距对应属性为android:layout_marginTop、android:layout_marginBottom、android:layout_marginLeft、android:layout_marginRight;内部边距对应属性为android:paddingTop、android:paddingBottom、android:paddingLeft、android:paddingRight。
下图所示为外部间距以及内部间距的演示效果:
布局的xml代码参数为:
<!-- 最外层的布局背景为蓝色 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#00aaff"
android:orientation="vertical">
<!-- 中间层的背脊布局为黄色 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:background="#ffff99"
android:padding="60dp" >
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000" />
</LinearLayout>
</LinearLayout>
视图的对齐方式
对齐方式有外部对齐(android:layout_gravity)和内部对齐(android:gravity)两种。设置外部坐上对齐需要有|连接两种对齐方式android:layout_gravity="top|left"
;设置内部右下对齐也需要用|连接两种对齐方式android:gravity="right|bottom"
。
下图所示为外部对齐以及内部对齐的演示效果:
布局的xml代码参数为:
<?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="300dp"
android:background="#ffff99"
android:padding="5dp" >
<!-- 第一个子布局背景为红色,它在上级视图中朝下对齐,它的下级视图则靠左对齐 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:layout_gravity="bottom"
android:background="#ff0000"
android:layout_margin="10dp"
android:padding="10dp" >
<!-- 内部视图的宽度和高度都是100dp,且背景为青色 -->
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ffff" />
</LinearLayout>
<!-- 第二个布局背景为红色,它在上级视图中朝上对齐,它的下级视图则靠右对齐 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="1"
android:layout_gravity="top"
android:gravity="right"
android:background="#ff0000"
android:layout_margin="10dp"
android:padding="10dp" >
<!-- 内部视图的宽度和高度都是100dp,且背景色为青色 -->
<View
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#00ffff" />
</LinearLayout>
</LinearLayout>
常用布局
线性布局LinearLayout
布局方式android:orientation
有垂直vertical
和水平horizontal
两种。如果LinearLayout不指定具体方向,则默认为horizontal方向。
下图所示为水平布局以及垂直对齐的演示效果:
布局的xml代码参数为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="横排第一个" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="横排第二个" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="竖排第一个" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="竖排第二个" />
</LinearLayout>
</LinearLayout>
线性布局除了以水平和垂直方向布局外,还可以通过android:layout_weight
属性设定控件空间所占比重。在哪个布局方向上设置空间占用比重,则需要将方向上的值设为0dp
。
下图所示为水平布局以及垂直对齐空间占比为1:1的演示效果:
布局的xml代码参数为:
<?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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="横排第一个" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="横排第二个" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#00ffff"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="竖排第一个" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="竖排第二个" />
</LinearLayout>
</LinearLayout>
相对布局RelativeLayout
相对布局的参照物有两种:一种时上级视图;另一种是和自身平级的视图。相对位置属性说明参见下表:
相对位置的属性取值 | 相对位置说明 |
---|---|
layout_toLeftOf | 当前视图在指定视图的左边 |
layout_toRightOf | 当前视图在指定视图的右边 |
layout_above | 当前视图在指定视图的上方 |
layout_below | 当前视图在指定视图的下方 |
layout_alignLeft | 当前视图与指定视图左对齐 |
layout_alignRight | 当前视图与指定视图右对齐 |
layout_alignTop | 当前视图与指定视图顶部对齐 |
layout_alignBottom | 当前视图与指定视图底部对齐 |
layout_centerInParent | 当前视图在上级视图中间 |
layout_centerHorizontal | 当前视图在上级视图的水平方向居中 |
layout_centerVertical | 当前视图在上级视图的垂直方向居中 |
layout_alignParentLeft | 当前视图与上级视图的左侧对齐 |
layout_alignParentRight | 当前视图与上级视图的右侧对齐 |
layout_alignParentTop | 当前视图与上级视图的顶部对齐 |
layout_alignParentBottom | 当前视图与上级视图的底部对齐 |
相对布局演示效果如下:
布局的xml代码参数为:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp">
<TextView
android:id="@+id/tv_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#eeeeee"
android:text="我在中间" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#eeeeee"
android:text="我在水平中间" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#eeeeee"
android:text="我在垂直中间" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#eeeeee"
android:text="我跟上级左边对齐" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#eeeeee"
android:text="我跟上级右边对齐" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#eeeeee"
android:text="我跟上级顶部对齐" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#eeeeee"
android:text="我跟上级底部对齐" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/tv_center"
android:layout_alignTop="@+id/tv_center"
android:background="#eeeeee"
android:text="我在中间左边" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv_center"
android:background="#eeeeee"
android:text="我在中间右边" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tv_center"
android:layout_alignLeft="@+id/tv_center"
android:background="#eeeeee"
android:text="我在中间上面" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_center"
android:layout_alignRight="@+id/tv_center"
android:background="#eeeeee"
android:text="我在中间下面" />
</RelativeLayout>
网格布局GridLayout
网格布局支持多行多列的表格布局,网格布局默认默认从左往右、从上往下排列。它的android:columnCount
属性指定网格的列数,即每行能放多少个视图;android:rowCount
属性指定了网格的行数,即每列能放多少个视图。
网格布局演示效果如下:
布局的xml代码参数为:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2" >
<TextView
android:layout_width="180dp"
android:layout_height="60dp"
android:gravity="center"
android:background="#ffcccc"
android:text="浅红色" />
<TextView
android:layout_width="180dp"
android:layout_height="60dp"
android:gravity="center"
android:background="#ffaa00"
android:text="橙色" />
<TextView
android:layout_width="180dp"
android:layout_height="60dp"
android:gravity="center"
android:background="#00ff00"
android:text="绿色" />
<TextView
android:layout_width="180dp"
android:layout_height="60dp"
android:gravity="center"
android:background="#660066"
android:text="深紫色" />
</GridLayout>
滚动视图ScrollView
滚动视图有两种:
ScrollView:它是垂直方向的滚动视图,垂直方向滚动时android:layout_width
属性设置值为match_parent
,android:layout_height
属性设置为wrap_content
。
HorizontalScrollView:他是水平方向的滚动视图,水平方向滚动时android:layout_width
属性设置值为wrap_content
,android:layout_height
属性设置为match_parent
。
有时ScrollView的实际内容不够而又需要将内容填充屏幕时,可以增加一个android:fillViewport
属性并且将其值设置为true
即可。
滚动视图演示效果如下:
布局的xml代码参数为:
<?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:orientation="vertical">
<!-- HorizontalScrollView是水平方向的滚动视图,当前高度为200dp -->
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="200dp" >
<!-- 水平方向的线性布局,两个子视图的颜色分别为青色和黄色 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="300dp"
android:layout_height="match_parent"
android:background="#aaffff" />
<View
android:layout_width="300dp"
android:layout_height="match_parent"
android:background="#ffff00" />
</LinearLayout>
</HorizontalScrollView>
<!-- ScrollView是垂直方向的滚动视图,当前高度为自适应 -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<!-- 垂直方向的线性布局,两个视图的颜色分别为绿色和橙色 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#00ff00" />
<View
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#ffffaa" />
</LinearLayout>
</ScrollView>
</LinearLayout>
按钮触控
按钮控件Button
Button控件派生于TextView,拥有TextView控件的属性和方法。不同的是Button有默认背景,内部文本自动居中对齐,下面介绍几个常用的属性。
android:textAllCaps属性
按钮的字母会默认转成大写。例如在xml的android:text
属性上写Hello World
,会显示成HELLO WORLD。但是貌似Android Studio更新到最新版后,已经不会这样了。
该值属性默认值true
,表示转换大写。如果当前使用的IDE版本还是这种默认转大写的特性,并且不希望默认转大写,可以将这个属性值设置为false
。
android:onClick属性
这个属性指示了按键点击时执行的函数,你可以在页面所在的java代码添加对应的回调函数,然后将函数名复制属性值android:onClick="doClick"
就行了。函数在代码中的定义如下:
public void doClick(View view){
//do something
}
点击事件
除了通过android:onClick
属性注册回调函数的方法外,我们还可以自己通过setOnClickListener
方法注册监听器,一般实际操作也是用这种方法居多。参考代码如下:
package com.example.chapter03;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.chapter03.util.DateUtil;
public class ButtonClickActivity extends AppCompatActivity implements View.OnClickListener{
//声明tv_result视图实例
private TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_click);
//获取名为tv_result的文本视图
tv_result = findViewById(R.id.tv_result);
//获取名为btn_click_single的按键控件并注册监听器
findViewById(R.id.btn_click_single).setOnClickListener(new MyOnClickListener());
//获取名为btn_click_public的按键控件并注册监听器
findViewById(R.id.btn_click_public).setOnClickListener(this);
}
//定义一个实现View.OnClickListener接口的监听器
class MyOnClickListener implements View.OnClickListener{
@Override
public void onClick(View view) { //点击事件的处理方法
String desc = String.format("%s 您点击了按钮:%s",
DateUtil.getNowTime(), ((Button) view).getText());
tv_result.setText(desc); // 设置文本视图的文本内容
}
}
@Override
public void onClick(View view) { //点击事件处理方法
//判断是否来自btn_click_public按键
if(view.getId() == R.id.btn_click_public){
String desc = String.format("%s 您点击了按钮:%s",
DateUtil.getNowTime(), ((Button) view).getText());
tv_result.setText(desc); // 设置文本视图的文本内容
}
}
}
页面布局xml代码如下:
<?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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_click_single"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="指定单独的点击监听器"
android:textColor="@color/black"
android:textSize="15sp" />
<Button
android:id="@+id/btn_click_public"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="指定公共的点击监听器"
android:textColor="@color/black"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="这里查看按钮的点击结果"
android:textColor="@color/black"
android:textSize="15sp" />
</LinearLayout>
长按事件
除了按键监听,还可以通过setOnLongClickListener
接口注册长按监听,操作代码图如下:
package com.example.chapter03;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.chapter03.util.DateUtil;
public class ButtonLongclickActivity extends AppCompatActivity implements View.OnLongClickListener{
//定义一个tv_result文本视图
private TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_longclick);
//获取名为tv_result的文本控件
tv_result = findViewById(R.id.tv_result);
//获取名为btn_longclick_single的按键控件并注册长按监听器
findViewById(R.id.btn_longclick_single).setOnLongClickListener(new MyOnLongClickListener());
//获取名为btn_longclick_public的按键并注册长按监听器
findViewById(R.id.btn_longclick_public).setOnLongClickListener(this);
}
//定义一个实现View.OnLongClickListener接口的长按监听器
class MyOnLongClickListener implements View.OnLongClickListener{
@Override
public boolean onLongClick(View view) { //长按事件的处理方法
String desc = String.format("%s 您长按了按钮:%s",
DateUtil.getNowTime(), ((Button) view).getText());
tv_result.setText(desc); // 设置文本视图的文本内容
return true;
}
}
//实现长按监听方法
@Override
public boolean onLongClick(View view) {
if (view.getId() == R.id.btn_longclick_public) { // 来自按钮btn_longclick_public
String desc = String.format("%s 您长按了按钮:%s",
DateUtil.getNowTime(), ((Button) view).getText());
tv_result.setText(desc); // 设置文本视图的文本内容
}
return true;
}
}
页面对应xml代码:
<?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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_longclick_single"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="指定单独的长按监听器"
android:textColor="@color/black"
android:textSize="15sp" />
<Button
android:id="@+id/btn_longclick_public"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="指定公共的长按监听器"
android:textColor="@color/black"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="这里查看按钮的长按结果"
android:textColor="@color/black"
android:textSize="15sp" />
</LinearLayout>
禁用与恢复按钮
在登录账号等场景,可能我们不能一来就能点击登录按钮,而是要等待密码的输入后才能点击。那么此时就可以将android:enabled
属性设置为false
失能掉按钮,待到满足条件时再通过setEnabled
接口设置按钮为true
使能状态。参考代码如下:
package com.example.chapter03;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.chapter03.util.DateUtil;
public class ButtonEnableActivity extends AppCompatActivity implements View.OnClickListener{
//声明一个名为tv_result的文本实例
private TextView tv_result;
//声明一个名为btn_test的按钮实例
private Button btn_test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button_enable);
//获取名为tv_result的文本视图
tv_result = findViewById(R.id.tv_result);
//对按钮注册监听器
findViewById(R.id.btn_enable).setOnClickListener(this);
findViewById(R.id.btn_disable).setOnClickListener(this);
//获取名为btn_test的按钮控件
btn_test = findViewById(R.id.btn_test);
btn_test.setOnClickListener(this);
}
@Override
public void onClick(View view) {
// 点击了按钮“启用测试按钮”
if (view.getId() == R.id.btn_enable) { //点击了按钮“启用测试按钮”
btn_test.setTextColor(Color.BLACK); //设置按钮的文字颜色
btn_test.setEnabled(true); // 启用当前控件
} else if(view.getId() == R.id.btn_disable) { //点击了按钮“禁用测试按钮”
btn_test.setTextColor(Color.GRAY); // 设置按钮的文字颜色
btn_test.setEnabled(false);//禁用当前控件
} else if (view.getId() == R.id.btn_test) { // 点击了按钮“测试按钮”
String desc = String.format("%s 您点击了按钮:%s",
DateUtil.getNowTime(), ((Button) view).getText());
tv_result.setText(desc); // 设置文本视图的文本内容
}
}
}
布局xml代码:
<?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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_enable"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="启动测试按钮" />
<Button
android:id="@+id/btn_disable"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="禁用测试按钮" />
</LinearLayout>
<Button
android:id="@+id/btn_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="测试按钮" />
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="这里查看测试按钮的点击效果" />
</LinearLayout>
图像显示
图像视图ImageView
显示图片可以使用ImageView控件。设置显示图片可以通过设置资源文件到android:src
属性,使用形式为android:src="@drawable/apple"
;也可以通过代码使用setImageResource
接口设置图片,接口传参使用形式为ImageView#setImageResource(R.drawable.apple)
。ImageView控件还涉及到多个缩放类型,默认类型是缩放类型为fitCenter
,所有类型说明如下表:
xml中的缩放类型 | ScaleType类中的缩放类型 | 说明 |
---|---|---|
fitXY | FIT_XY | 拉伸图片使其正好填满视图(图片可能会被拉伸和变形) |
fitStart | FIT_START | 保持宽高比,拉伸图片使其位于视图上方或左侧 |
fitCenter | FIT_CENTER | 保持宽高比例,拉伸图片使其位于视图中间(取值默认是这个类型) |
fitEnd | FIT_END | 保持宽高比,拉伸图片使其位于视图的下方或右侧 |
center | CENTER | 保持图片原尺寸,使其位于视图中间 |
centerCrop | CENTER_CROP | 拉伸图片使其充满视图,并位于视图中间 |
centerInside | CENTER_INSIDE | 保持宽高比,缩小图片使其位于视图中间(只缩小不放大) |
演示代码如下:
package com.example.chapter03;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class ImageScaleActivity extends AppCompatActivity implements View.OnClickListener{
// 声明图像视图对象
private ImageView iv_scale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_scale);
// 获取名为iv_scale的图像视图对象
iv_scale = findViewById(R.id.iv_scale);
// 注册按键监听器演示不同缩放类型效果
findViewById(R.id.btn_center).setOnClickListener(this);
findViewById(R.id.btn_fitCenter).setOnClickListener(this);
findViewById(R.id.btn_centerCrop).setOnClickListener(this);
findViewById(R.id.btn_centerInside).setOnClickListener(this);
findViewById(R.id.btn_fitXY).setOnClickListener(this);
findViewById(R.id.btn_fitStart).setOnClickListener(this);
findViewById(R.id.btn_fitEnd).setOnClickListener(this);
}
@Override
public void onClick(View view) { // 点击事件处理方法
// 设置图像视图的图片资源
iv_scale.setImageResource(R.drawable.apple);
if(view.getId() == R.id.btn_center) {
// 将缩放类型设置为“按照原尺寸居中显示”
iv_scale.setScaleType(ImageView.ScaleType.CENTER);
} else if (view.getId() == R.id.btn_fitCenter) {
//将缩放类型设置为“保持宽高比例,缩放图片使其位于视图中间”
iv_scale.setScaleType(ImageView.ScaleType.FIT_CENTER);
} else if (view.getId() == R.id.btn_centerCrop) {
// 将缩放类型设置为“缩放图片使其充满视图,并位于视图中间”
iv_scale.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else if (view.getId() == R.id.btn_centerInside) {
// 将缩放类型设置为“保持宽高比例,缩小图片使之位于视图中间(只缩小不放大)”
iv_scale.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
} else if (view.getId() == R.id.btn_fitXY) {
// 将缩放类型设置为“缩放图片使其正好填满视图(图片可能被缩放变形)”
iv_scale.setScaleType(ImageView.ScaleType.FIT_XY);
} else if (view.getId() == R.id.btn_fitStart) {
// 将缩放类型设置为“保持宽高比例,缩放图片使其位于视图上方或左侧”
iv_scale.setScaleType(ImageView.ScaleType.FIT_START);
} else if (view.getId() == R.id.btn_fitEnd) {
// 将缩放类型设置为“保持宽高比例,缩放图片使其位于视图下方或右侧”
iv_scale.setScaleType(ImageView.ScaleType.FIT_END);
}
}
}
xml布局代码:
<?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:orientation="vertical">
<ImageView
android:id="@+id/iv_scale"
android:layout_width="match_parent"
android:layout_height="220dp"
android:src="@drawable/apple" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_fitCenter"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="fitCenter"
android:textColor="@color/black"
android:textSize="14sp" />
<Button
android:id="@+id/btn_centerCrop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="centerCrop"
android:textColor="@color/black"
android:textSize="14sp" />
<Button
android:id="@+id/btn_centerInside"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="centerInside"
android:textColor="@color/black"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="center"
android:textColor="@color/black"
android:textSize="14sp" />
<Button
android:id="@+id/btn_fitXY"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="fitXY"
android:textColor="@color/black"
android:textSize="14sp" />
<Button
android:id="@+id/btn_fitStart"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="fitStart"
android:textColor="@color/black"
android:textSize="14sp" />
<Button
android:id="@+id/btn_fitEnd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="fitEnd"
android:textColor="@color/black"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
图像按钮ImageButton
ImageButton继承自ImageView,拥有ImageView的所有方法。同时作为按钮,按钮拥有的点击事件和长按事件它也是具备的。
ImageButton和Button的区别有以下几点:
1、Button既可以显示图片也可以显示文字,ImageButton只能显示图片不能显示文本。
2、ImageButton上的图像可按比例缩放,而Button通过背景设置的图片会拉伸变形。
3、Button只能显示一张图片,ImageButton可分别在前景和背景显示图片,实现两张图片叠加效果。
常用属性使用实例如下:
<ImageButton
android:layout_width="match_parent"
android:layout_height="80dp"
android:src="@drawable/sqrt"
android:scaleType="fitCenter" />
同时展示文本与图像
使用Button控件可实现在图标旁边放置文字的功能。Button提供了几个与图标有关的属性,通过这些属性即可指定文字环绕模式,相关说明如下:
xml属性 | 说明 |
---|---|
drawableLeft | 指定文字左边的图标 |
drawableRight | 指定文字右边的图标 |
drawableTop | 指定文字上边的图标 |
drawableBottom | 指定文字下边的图标 |
drawablePadding | 指定文字与图标的间距 |
实际使用效果如下图:
xml布局代码如下:
<?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:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:drawableLeft="@drawable/ic_about"
android:drawablePadding="5dp"
android:text="图标在左"
android:textColor="@color/black"
android:textSize="17sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:drawableRight="@drawable/ic_about"
android:drawablePadding="5dp"
android:text="图标在右"
android:textColor="@color/black"
android:textSize="17sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:drawableTop="@drawable/ic_about"
android:drawablePadding="5dp"
android:text="图标在上"
android:textColor="@color/black"
android:textSize="17sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:drawableBottom="@drawable/ic_about"
android:drawablePadding="5dp"
android:text="图标在下"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
工程源码
所有代码可点击工程源码下载。