Simliar与iOS的行动表相当于Kotlin
import com.google.android.material.bottomsheet.BottomSheetDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomSheetDialog = BottomSheetDialog(this)
val bottomSheetView = this.layoutInflater.inflate(R.layout.bottom_sheet_layout, null)
bottomSheetDialog.setContentView(bottomSheetView)
actionSheetButton.setOnClickListener {
showDialogNotificationAction(bottomSheetDialog)
}
bottomSheetView.button1.setOnClickListener {
Toast.makeText(this, "Button 1 Clicked", Toast.LENGTH_LONG).show()
}
bottomSheetView.button2.setOnClickListener {
Toast.makeText(this, "Button 2 Clicked", Toast.LENGTH_LONG).show()
}
bottomSheetView.button3.setOnClickListener {
Toast.makeText(this, "Button 3 Clicked", Toast.LENGTH_LONG).show()
}
bottomSheetView.button4.setOnClickListener {
Toast.makeText(this, "Button 4 Clicked", Toast.LENGTH_LONG).show()
}
bottomSheetView.cancelAttachment.setOnClickListener {
bottomSheetDialog.dismiss()
}
}
private fun showDialogNotificationAction(bottomSheetDialog: BottomSheetDialog) {
bottomSheetDialog.show()
val bottomSheetDialogFrameLayout =
bottomSheetDialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
bottomSheetDialogFrameLayout?.background = null
}
bottom\u sheet\u布局。xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_sheet_rounded_corner"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="41dp"
android:foreground="?attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="14dp"
android:text="Android Action Sheet"
android:textColor="#909090"
android:textSize="12sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#D1D1CF" />
<LinearLayout
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="52dp"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button 1"
android:textColor="#007CFE"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#D1D1CF" />
<LinearLayout
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="52dp"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button 2"
android:textColor="#007CFE"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#D1D1CF" />
<LinearLayout
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="52dp"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button 3"
android:textColor="#007CFE"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#D1D1CF" />
<LinearLayout
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="52dp"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button 4"
android:textColor="#007CFE"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/cancelAttachment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@drawable/bottom_sheet_rounded_corner"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Cancel"
android:textColor="#FFFFFF"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
底部_sheet_rounded_角。xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<corners android:radius="10dp" />
</shape>