• Android Video Tutorials

Android - ImageButton 控制元件



ImageButton 是一個 AbsoluteLayout,它允許您指定其子元素的確切位置。這顯示了一個帶有影像(而不是文字)的按鈕,使用者可以按下或點選它。

Image Button

Android 按鈕樣式集

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 指示 tutorialspoint 的影像
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>

讓我們嘗試執行您的 **myapplication** 應用程式。我假設您在進行環境設定時建立了您的 **AVD**。要從 Android Studio 執行應用程式,請開啟專案的活動檔案之一,然後單擊工具欄中的執行 Eclipse Run Icon 圖示。Android Studio 將應用程式安裝到您的 AVD 上並啟動它,如果您的設定和應用程式一切正常,它將顯示以下模擬器視窗 -

ImageButton

單擊 ImageButton 後將顯示以下螢幕,它顯示了一個吐司訊息。

Toast Message

練習

我建議您嘗試在佈局 XML 檔案和程式設計時使用 ImageButton 的不同屬性來嘗試以上示例,以獲得 ImageButton 不同的外觀和感覺。嘗試使其可編輯,更改字型顏色、字體系列、寬度、textSize 等,並檢視結果。您還可以嘗試在單個活動中使用多個 ImageButton 控制元件的上述示例。

android_user_interface_controls.htm
廣告

© . All rights reserved.