Android: ImageButton trong Android
Một ImageButton là một AbsoluteLayout mà cho phép bạn xác định vị trí chính xác của các view con. Control này hiển thị một image (thay cho text) mà có thể được nhấn hoặc click bởi người dùng.
BUTTON STYLE SET TRONG ANDROID
Các thuộc tính của ImageButton trong Android
Bảng dưới liệt kê một số thuộc tính quan trọng liên quan tới ImageButton Control.
Kế thừa từ lớp android.widget.ImageView:
Thuộc tính | Miêu tả |
---|---|
android:adjustViewBounds | Thiết lập là true nếu bạn muốn ImageView để điều chỉnh biên giới của nó để bảo quản tỷ lệ các cạnh của drawable |
android:baseline | Đây là offset của baseline bên trong view này |
android:baselineAlignBottom | Nếu true, view sẽ được căn chỉnh baseline dựa trên cạnh dưới của nó |
android:cropToPadding | Nếu true, hình ảnh sẽ bị cắt để phù hợp với padding của nó |
android:src | Thiết lập một drawable như là nội dung của ImageView này |
Kế thừa từ lớpandroid.view.View:
Thuộc tính | Miêu tả |
---|---|
android:background | Đây là một drawable để sử dụng như background |
android:contentDescription | Định nghĩa text miêu tả ngắn gọn nội dung của view |
android:id | Cung cấp một tên định danh cho view này |
android:onClick | Đây là tên phương thức trong ngữ cảnh của View này để triệu hồi khi view được click |
android:visibility | Điều khiển sự nhìn thấy ban đầu của view |
Ví dụ
Ví dụ sau sẽ minh họa cách tạo ứng dụng Androidd bằng cách sử dụng Linear Layout và ImageButton.
Bước | Miêu tả |
---|---|
1 | Bạn sử dụng Android Studio IDE để tạo một ứng dụng Android |
2 | Sửa đổi file src/MainActivity.java để thêm một click event |
2 | Sửa đổi nội dung mặc định của file res/layout/activity_main.xml để bao gồm UI Control |
3 | Không cần thay đổi strings.xml |
4 | Chạy ứng dụng để chạy Android Emulator và kiểm tra kết quả các thay đổi đã thực hiện trong ứng dụng |
Sau đây là nội dung của file Main Activity:
package v1study.com.imagebutton; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ImageButton imageButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View view){ Toast.makeText(getApplicationContext(),"You clicked ImageButton Control",Toast.LENGTH_LONG).show(); } }
Sau đây là nội dung của file res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/imagebuttonv1study" android:textColor="#4CAF50" android:textSize="30sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.495" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.146" /> <ImageButton android:id="@+id/imageButton" android:layout_width="232dp" android:layout_height="221dp" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="8dp" android:contentDescription="@string/logo_v1study" android:onClick="click" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.5" app:srcCompat="@drawable/logo_v1_regular" /> </androidx.constraintlayout.widget.ConstraintLayout>
Sau đây là nội dung của res/values/strings.xml:
<resources> <string name="app_name">ImageButtonV1Study</string> <string name="imagebuttonv1study">ImageButtonV1Study</string> <string name="logo_v1study">Logo V1Study</string> </resources>
Sau đây là nội dung mặc định của AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:dist="http://schemas.android.com/apk/distribution" package="v1study.com.imagebutton"> <dist:module dist:instant="true" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Chạy ứng dụng:
Màn hình sau sẽ xuất hiện khi bạn click vào ImageButton: