我這邊做因為數據是動態獲取的,本來想用recyclerview來做,突然想起來某個版本加了這東西。看演示,別人的是這樣的,
我做完是這樣,注意這不是全選中,而是全沒選中的樣子。。
image.png
image.png
然后查了下官網,發現要對MaterialButtonToggleGroup
內部的MaterialButton
加上style="?attr/materialButtonOutlinedStyle"
的樣式。
因為我是動態添加的,所以沒有在xml寫了,代碼是這樣的
btnToggleGroup.addView(createBtnToggle( "-"))
private fun createBtnToggle(content: String): Button {
val btn = MaterialButton(
requireContext(),
null,
R.attr.materialButtonOutlinedStyle
)
val layoutParam = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
btn.layoutParams = layoutParam
btn.setPadding(16f.dp.toInt(), 8f.dp.toInt(), 16f.dp.toInt(), 8f.dp.toInt())
btn.text = content
btn.textSize = 20f.sp
return btn
}
如果在xml里用,那直接官網上這樣就行
<com.google.android.material.button.MaterialButtonToggleGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toggle_button_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.google.android.material.button.MaterialButton
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_label_private"/>
<com.google.android.material.button.MaterialButton
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_label_team"/>
<com.google.android.material.button.MaterialButton
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_label_everyone"/>
<com.google.android.material.button.MaterialButton
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_label_custom"/>
</com.google.android.material.button.MaterialButtonToggleGroup>