MaterialButtonToggleGroup中按鈕樣式

我這邊做因為數(shù)據(jù)是動態(tài)獲取的,本來想用recyclerview來做,突然想起來某個版本加了這東西。看演示,別人的是這樣的,


我做完是這樣,注意這不是全選中,而是全沒選中的樣子。。


image.png

image.png

然后查了下官網(wǎng),發(fā)現(xiàn)要對MaterialButtonToggleGroup內(nèi)部的MaterialButton加上style="?attr/materialButtonOutlinedStyle"的樣式。
因為我是動態(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>
 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 用兩張圖告訴你,為什么你的 App 會卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 12,970評論 2 59
  • 第一章 jQuery簡介 1-1 jQuery簡介 1.簡介 2.優(yōu)勢 3.特性與工具方法 1-2 環(huán)境搭建 進入...
    mo默22閱讀 1,617評論 0 11
  • Android基礎(chǔ)及相關(guān)機制 Android Context 上下文 你必須知道的一切 Android中子線程真的...
    楷桐閱讀 2,019評論 1 30
  • 用到的組件 1、通過CocoaPods安裝 2、第三方類庫安裝 3、第三方服務(wù) 友盟社會化分享組件 友盟用戶反饋 ...
    SunnyLeong閱讀 14,708評論 1 180
  • 久違的晴天,家長會。 家長大會開好到教室時,離放學(xué)已經(jīng)沒多少時間了。班主任說已經(jīng)安排了三個家長分享經(jīng)驗。 放學(xué)鈴聲...
    飄雪兒5閱讀 7,574評論 16 22