คู่มือนี้อธิบายวิธีเพิ่มการโต้ตอบที่กำหนดเองลงในแท็บที่กำหนดเอง
เปิดใช้การดำเนินการแชร์เริ่มต้น
หากคุณไม่ได้ระบุการดำเนินการแชร์ที่กำหนดเอง เราขอแนะนำให้เปิดใช้การดำเนินการแชร์เริ่มต้นของเบราว์เซอร์ในเมนูรายการเพิ่มเติมเพื่อให้ผู้ใช้แชร์ลิงก์ไปยังเนื้อหาที่กำลังดูได้ง่ายขึ้น ดังนี้
val intentBuilder = CustomTabsIntent.Builder()
intentBuilder.setShareState(CustomTabsIntent.SHARE_STATE_ON)
เพิ่มปุ่มการกระทำที่กำหนดเอง
สำหรับการดำเนินการที่สำคัญ แถบเครื่องมือแท็บที่กำหนดเองจะช่วยให้คุณผสานรวมปุ่มการดำเนินการที่กำหนดเองได้ ซึ่งอาจมีป้ายกำกับข้อความหรือไอคอนที่กำหนดเอง ไอคอนควรมีความสูง 24 dp และความกว้าง 24-48 dp

เช่น คุณสามารถเพิ่มการดำเนินการแชร์ที่กำหนดเองลงในแถบเครื่องมือได้ โดยสร้าง BroadcastReceiver ซึ่งจะได้รับการเรียกใช้เมื่อผู้ใช้คลิกการแชร์ในแท็บที่กำหนดเอง
ลงทะเบียน BroadCastReceiver ในไฟล์ AndroidManifest.xml โดยทำดังนี้
<application …>
<receiver android:name=".ShareBroadcastReceiver" />
</application>
จากนั้นเพิ่มคลาสใหม่ ShareBroadcastReceiver ในเมธอด onReceive() ให้ดึงข้อมูล URL ที่แสดงอยู่ในปัจจุบันจาก Intent และทริกเกอร์ Intent การส่ง
class ShareBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val url = intent.dataString
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_TEXT, url)
sendIntent.type = "text/plain"
val shareIntent = Intent.createChooser(sendIntent, null)
shareIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(shareIntent)
}
}
ตอนนี้ ให้สร้าง PendingIntent สำหรับ ShareBroadcast และลงทะเบียนผ่าน setActionButton() ส่ง Intent รอดำเนินการพร้อมกับไอคอนและคำอธิบาย
val shareDescription = getString(R.string.label_action_share)
val shareIcon = BitmapFactory.decodeResource(
resources,
android.R.drawable.ic_menu_share
)
// Create a PendingIntent to your BroadCastReceiver implementation
val actionIntent = Intent(
this.applicationContext,
ShareBroadcastReceiver::class.java
)
val pendingIntent = PendingIntent.getBroadcast(
applicationContext,
0 /* request code */,
actionIntent,
PendingIntent.FLAG_MUTABLE
)
// Set the pendingIntent as the action to be performed when the button is clicked.
val intentBuilder = CustomTabsIntent.Builder()
// …
.setActionButton(shareIcon, shareDescription, pendingIntent)
.build()
เพิ่มรายการเมนูที่กำหนดเอง
แท็บที่กำหนดเองมีการดำเนินการเริ่มต้นสูงสุด 5 รายการที่เบราว์เซอร์มีให้ ได้แก่ "ไปข้างหน้า" "ข้อมูลหน้าเว็บ" "รีเฟรช" "ค้นหาในหน้าเว็บ" และ "เปิดในเบราว์เซอร์" นอกจากนี้ คุณยังเพิ่มได้อีกสูงสุด 7 รายการ ระบบจะแทรกรายการเมนูเหล่านี้ระหว่างแถวไอคอนและรายการที่เบราว์เซอร์มีให้ (ดูรูปภาพด้านล่าง) จำนวนจริงจะขึ้นอยู่กับการใช้งานเบราว์เซอร์ที่เกี่ยวข้อง (เช่น ในเวอร์ชัน 117 Chrome ได้เพิ่มจำนวนรายการเมนูจาก 5 เป็น 7 รายการ) ดังนั้น คุณจึงควรเพิ่มรายการที่สำคัญที่สุดก่อน
คุณเข้าถึงการกระทำที่กำหนดเองได้ผ่านเมนู 3 จุดที่มุมขวาบน โดยทำดังนี้

หากต้องการเพิ่มรายการในเมนู ให้เรียกใช้ CustomTabsIntent.Builder.addMenuItem() พร้อมชื่อและ PendingIntent เมื่อผู้ใช้แตะรายการในเมนู เบราว์เซอร์จะเริ่มการทำงานของ PendingIntent
val intent = CustomTabsIntent.Builder()
// ...
.addMenuItem("Menu item 1", pendingIntent)
.addMenuItem("Menu item 2", pendingIntent)
.addMenuItem("Menu item 3", pendingIntent)
.addMenuItem("Menu item 4", pendingIntent)
.addMenuItem("Menu item 5", pendingIntent)
.build()
ปรับแต่งปุ่มปิด
ปรับแต่งปุ่มปิดเพื่อให้แท็บที่กำหนดเองเข้ากับขั้นตอนของแอปได้ดียิ่งขึ้น หากต้องการให้ผู้ใช้รู้สึกว่าแท็บที่กำหนดเองเป็นกล่องโต้ตอบโมดัล ให้ใช้ปุ่ม “X” เริ่มต้น หากต้องการให้ผู้ใช้รู้สึกว่าแท็บที่กำหนดเองเป็นส่วนหนึ่งของขั้นตอนการใช้งานแอปพลิเคชัน ให้ใช้ลูกศรย้อนกลับ
val intentBuilder = CustomTabsIntent.Builder()
intentBuilder.setCloseButtonIcon(
BitmapFactory.decodeResource(resources, R.drawable.ic_arrow_back)
)
เพิ่มแถบเครื่องมือด้านล่าง
แถบเครื่องมือด้านล่างเป็นวิธีที่ยืดหยุ่นมากในการเพิ่มฟังก์ชันการทำงานให้กับแท็บที่กำหนดเอง

การส่งออบเจ็กต์ RemoteViews ไปยัง CustomTabIntent.Builder.setSecondaryToolbarViews() จะช่วยให้ปรับแต่งแถบเครื่องมือด้านล่างได้อย่างเต็มที่และอัปเดตแบบไดนามิก
ก่อนอื่น ให้ประกาศเลย์เอาต์แถบเครื่องมือโดยสร้างไฟล์เลย์เอาต์ใหม่ res/layout/custom_tab_toolbar.xml ดังนี้
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ct_toolbar_next"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_margin="8dp"
android:padding="8dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="Next" />
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ct_toolbar_previous"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_margin="8dp"
android:padding="8dp"
android:text="Previous" />
</LinearLayout>
ขั้นตอนถัดไปคือการลงทะเบียน BroadcastReceiver ซึ่งจะจัดการการโต้ตอบกับแถบเครื่องมือในไฟล์ AndroidManifest.xml ดังนี้
<application …>
<receiver android:name=".BottomToolbarBroadcastReceiver" />
</application>
จากนั้นใช้ BroadcastReceiver ซึ่งจะจัดการการโต้ตอบทั้งหมดกับแถบเครื่องมือด้านล่าง
class BottomToolbarBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val url = intent.dataString
val remoteViewId = intent.getIntExtra(CustomTabsIntent.EXTRA_REMOTEVIEWS_CLICKED_ID, -1)
if (remoteViewId == R.id.ct_toolbar_previous) {
// handle previous
} else if (remoteViewId == R.id.ct_toolbar_next) {
// handle next
}
}
}
สุดท้าย ให้ลงทะเบียนแถบเครื่องมือโดยทำดังนี้
// Create the pending intent
val actionIntent = Intent(
this.applicationContext,
BottomToolbarBroadcastReceiver::class.java
)
val toolbarPendingIntent = PendingIntent.getBroadcast(
applicationContext,
0 /* request code */,
actionIntent,
PendingIntent.FLAG_MUTABLE
)
// Pass the toolbar layout to the RemoteViews instance
val secondaryToolbarViews = RemoteViews(packageName, R.layout.custom_tab_toolbar)
// All toolbar buttons
val clickableIds = intArrayOf(R.id.ct_toolbar_next, R.id.ct_toolbar_previous)
// Register the bottom toolbar when creating a new custom tab intent
val intent = CustomTabsIntent.Builder()
.setSecondaryToolbarViews(secondaryToolbarViews, clickableIds, toolbarPendingIntent)
.build()
ปุ่มบุ๊กมาร์กและปุ่มดาวน์โหลด
ปุ่มบุ๊กมาร์กและปุ่มดาวน์โหลดในเมนู 3 จุดจะเปิดใช้อยู่โดยค่าเริ่มต้น หากต้องการปิดใช้ ให้ใช้โค้ดต่อไปนี้ใน CustomTabsIntent.Builder
val customTabsIntent = CustomTabsIntent.Builder()
.setBookmarksButtonEnabled(false)
.setDownloadButtonEnabled(false)
.build()
ถัดไป: ดูวิธีเพิ่มความเร็วในการโหลดเนื้อหาเว็บในแท็บที่กำหนดเอง