无涯教程-Android - ImageButton函数

ImageButton是一个AbsoluteLayout,可让您指定其子级的确切位置。这显示了带有图像(而不是文本)的按钮,用户可以按下或单击该按钮。

Android button style set

ImageButton属性

以下是与ImageButton控件相关的重要属性。您可以查看Android官方文档以获取属性的完整列表以及可以在运行时更改这些属性的相关方法。

继承自 android.widget.ImageView 类-

Sr.No Attribute & 描述
1 android:adjustViewBounds 如果希望ImageView调整其边界以保留其可绘制对象的宽高比,请将其设置为true。
2 android:baseline 这是该视图内基线的偏移量。
3 android:baselineAlignBottom 如果为true,则图像视图将根据其底边与基线对齐。
4 android:cropToPadding 如果为true,则图像将被裁剪以适合其填充。
5 android:src 这将可绘制对象设置为此ImageView的内容。

继承自 android.view.View 类-

Sr.No Attribute & 描述
1 android:background 这是一个可绘制的背景。
2 android:content描述 这定义了简短描述视图内容的文本。
3 android:id 这提供了该视图的标识符名称
4 android:onClick 这是单击该视图时在该视图中要调用的方法的名称。
5 android:visibility 这控制了视图的初始可见性。

示例

本示例将带您完成简单的步骤,以展示如何使用Linear Layout和ImageButton创建自己的Android应用程序。

以下是修改后的主要Activity文件 src/com.example.myapplication/MainActivity.java 的内容。

复制代码
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;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      imgButton =(ImageButton)findViewById(R.id.imageButton);
      imgButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"You download is 
               resumed",Toast.LENGTH_LONG).show();
         }
      });
   }
}

以下是 res/layout/activity_main.xml 文件的内容-

复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
   xmlns: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" />
      
   <ImageButton
      android: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" >
      
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android: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>

单击运行工具栏。 Android Studio将应用程序安装在您的AVD上并启动它,如果您的设置和应用程序一切正常,它将显示在"Emulator"窗口下面-

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

Android 中的 ImageButton函数 - 无涯教程网无涯教程网提供ImageButton是一个AbsoluteLayout,可让您指定其子级的确切位置。这显示了带有图像(而...https://www.learnfk.com/android/android-imagebutton-control.html

相关推荐
DogDaoDao5 小时前
Android 硬件编码器参数完全指南:MediaCodec 深度解析
android·音视频·视频编解码·h264·硬编码·视频直播·mediacodec
JohnnyDeng946 小时前
Android 自定义 View:Canvas 绘图与事件分发深度解析
android
Android小码家9 小时前
Framework之Launcher小窗开发
android·framework·虚拟屏·小窗
赏金术士9 小时前
第七章:状态管理实战与架构总结
android·ui·kotlin·compose
颂love10 小时前
MySQL的执行流程
android·数据库·mysql
云起SAAS15 小时前
抖音小游戏源码 - 消消乐 | 含激励广告+成就系统 | 开箱即用商业级消除游戏模板
android·游戏·广告联盟·看激励广告联盟流量主·抖音小游戏源码 - 消消乐
大貔貅喝啤酒16 小时前
基于Windows下载安装Android Studio 3.3.2版本教程(2026详细图文版)
android·java·windows·android studio
程序员码歌16 小时前
OpenSpec 到 Superpowers:AI 编码从说清到做对
android·前端·人工智能
2501_9151063216 小时前
深入解析无源码iOS加固原理与方案,保护应用安全
android·安全·ios·小程序·uni-app·cocoa·iphone
黄林晴20 小时前
重磅官宣:Android UI 开发正式进入 Compose-first 时代
android·google io