MaterialButtonToggleGroup中按钮样式

我这边做因为数据是动态获取的,本来想用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>
 
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,519评论 2 59
  • 第一章 jQuery简介 1-1 jQuery简介 1.简介 2.优势 3.特性与工具方法 1-2 环境搭建 进入...
    mo默22阅读 5,628评论 0 11
  • Android基础及相关机制 Android Context 上下文 你必须知道的一切 Android中子线程真的...
    楷桐阅读 6,112评论 1 30
  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 14,979评论 1 180
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 12,217评论 16 22

友情链接更多精彩内容