Android(安卓)ImageButton 控件
ImageButton 是一个 AbsoluteLayout,它让您可以指定其子级的确切位置。这显示了一个带有图像(而不是文本)的按钮,用户可以按下或单击该按钮。
ImageButton 属性
以下是与 ImageButton 控件相关的重要属性。您可以查看Android官方文档,以获取完整的属性列表和可用于更改这些属性的相关方法。
继承自 android.widget.ImageView 类:
| 编号 | 属性 & 描述 |
|---|---|
| 1 | android:adjustViewBounds 如果希望 ImageView 调整其边界以保持其可绘制的纵横比,请将此设置为 true。 |
| 2 | android:baseline 这是此视图中基线的偏移量。 |
| 3 | android:baselineAlignBottom 如果为 true,图像视图将基于其底边与基线对齐。 |
| 4 | android:cropToPadding 如果为 true,图像将被裁剪以适应其填充。 |
| 5 | android:src 这将可绘制的内容设置为此 ImageView 的内容。 |
继承自 android.view.View 类:
| 编号 | 属性 & 描述 |
|---|---|
| 1 | android:background 这是一个可作为背景使用的绘图。 |
| 2 | android:contentDescription 它定义了简要描述视图内容的文本。 |
| 3 | android:id 这提供了此视图的标识符名称 |
| 4 | android:onClick 这是单击视图时要调用的此视图上下文中方法的名称。 |
| 5 | android:visibility 这将控制视图的初始可见性。 |
实例
这个实例将带您完成简单的步骤,展示如何使用线性布局和 ImageButton 创建自己的 Android 应用程序。
| 步骤 | 描述 |
|---|---|
| 1 | 您将使用 Android studio IDE 创建一个 Android 应用程序,并将其命名为 com.example.myapplication 包下的 myapplication。如 Hello World 实例一章中所述 |
| 2 | 修改 src/MainActivity.java 文件添加一个点击事件。 |
| 3 | 修改 res/layout/activity_main.xml 文件的默认内容以包含 Android UI 控件。 |
| 4 | 无需在 Android 中定义默认常量,Android Studio 会处理默认常量。 |
| 5 | 运行应用程序以启动 Android 模拟器并验证应用程序中所做更改的结果。 |
以下是修改后的主活动文件 src/com.example.myapplication/MainActivity.java 的内容。此文件可以包括每个基本生命周期方法。
在下面的实例中,abc 表示 cankaoshouce 的图像
package com.example.myapplication;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.Toast;public class MainActivity extends Activity {ImageButton imgButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imgButton =(ImageButton)findViewById(R.id.imageButton);imgButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getApplicationContext(),"You download isresumed",Toast.LENGTH_LONG).show();}});}}
下面是 res/layout/activity_main.xml 文件的内容:
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context=".MainActivity"><TextView android:text="Tutorials Point"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="30dp"android:layout_alignParentTop="true"android:layout_alignRight="@+id/imageButton"android:layout_alignEnd="@+id/imageButton" /><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/imageButton"android:layout_centerVertical="true"android:layout_centerHorizontal="true"android:src="@drawable/abc"/></RelativeLayout>
下面是 res/values/strings.xml 文件的内容,定义了新常量:
<?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">myapplication</string></resources>
下面是 AndroidManifest.xml 的内容:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.myapplication" ><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.myapplication.MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
让我们尝试运行 myapplication 应用程序。我假设您在进行环境设置时创建了 AVD。要从 Android Studio 运行应用程序,请打开项目的 activity 文件之一,然后单击工具栏上的运行 Eclipse
图标。Android studio 在您的 AVD 上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口:

单击 ImageButton 后将出现以下屏幕,它显示一条消息。

练习
建议在编程时使用布局 XML 文件中 ImageButton 的不同属性来尝试上面的示例,以使 ImageButton 具有不同的外观。尝试使其可编辑,更改为字体颜色、字体系列、宽度、文本大小等,然后查看结果。您还可以在一个活动中使用多个 ImageButton 控件来尝试上面的示例。