我這邊做因?yàn)閿?shù)據(jù)是動態(tài)獲取的够傍,本來想用recyclerview來做甫菠,突然想起來某個(gè)版本加了這東西∶嵬停看演示寂诱,別人的是這樣的,
我做完是這樣安聘,注意這不是全選中痰洒,而是全沒選中的樣子瓢棒。。
然后查了下官網(wǎng)丘喻,發(fā)現(xiàn)要對MaterialButtonToggleGroup
內(nèi)部的MaterialButton
加上style="?attr/materialButtonOutlinedStyle"
的樣式脯宿。
因?yàn)槲沂莿討B(tài)添加的,所以沒有在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里用嗅绰,那直接官網(wǎng)上這樣就行
<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>