In this tutorial, we’ll be focusing on MaterialButtonToggleGroup, newly introduced with Android Material Components.

在本教程中,我们将重点介绍Android Material Components刚引入的MaterialButtonToggleGroup

Android MaterialButtonToggleGroup (Android MaterialButtonToggleGroup)

Also known as ToggleButton, this used to host buttons with checkable behavior. It resembles SegmentedControl in iOS. Users can select one or multiple choices from the group.

也称为ToggleButton,用于托管具有可检查行为的按钮。 它类似于iOS中的SegmentedControl。 用户可以从组中选择一个或多个选项。

In order to use Material Components, we need to import the following:

为了使用Material Components,我们需要导入以下内容:

implementation 'com.google.android.material:material:1.0.0'
MaterialComponent theme in your Activity.MaterialComponent主题。

Here’s how we define a MaterialButtonToggleGroup in XML:

这是我们在XML中定义MaterialButtonToggleGroup的方法:

<com.google.android.material.button.MaterialButtonToggleGroupandroid:id="@+id/toggleGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"app:checkedButton="@id/btnB"app:singleSelection="true"><Button.../><Buttonandroid:id="@+id/btnB".../></com.google.android.material.button.MaterialButtonToggleGroup>

app:singleSelection attribute is much like the RadioGroup.

app:singleSelection属性非常类似于RadioGroup 。

Material Components lets us style the following things on our UI:

通过Material Components,我们可以在UI上设置以下样式:

  • Colors色彩
  • Shapes形状
  • Typography版式

In the next section, we’ll customize our MaterialButtonToggleGroup with various shapes, color, and fonts!
Let’s get started with a fresh new Android Studio Project.

在下一节中,我们将使用各种形状,颜色和字体自定义MaterialButtonToggleGroup!
让我们开始一个全新的Android Studio项目。

项目结构 (Project Structure)

Android Material Button Toggle Group Project

Android Material Button切换小组项目

In the next sections, we’ll deal with different styles of MaterialButtonToggleGroups.

在接下来的部分中,我们将处理MaterialStyleToggleGroups的不同样式。

基本用法 (Basic Usage)

<com.google.android.material.button.MaterialButtonToggleGroupandroid:id="@+id/toggleGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"app:checkedButton="@id/btnAndroid"app:singleSelection="true"><Buttonandroid:id="@+id/btnAndroid"style="@style/Widget.MaterialComponents.Button.OutlinedButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Android" /><Buttonandroid:id="@+id/btniOS"style="@style/Widget.MaterialComponents.Button.OutlinedButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="iOS" /></com.google.android.material.button.MaterialButtonToggleGroup>

Android Material Toggle Basic

Android Material Toggle基本

The recommended style for Button in a MaterialButton is Widget.MaterialComponents.Button.OutlinedButton.

MaterialButton中Button的推荐样式是Widget.MaterialComponents.Button.OutlinedButton

带图标和文字 (With Icon and Text)

To use icons in Buttons, we’ll use MaterialButton:

要在Buttons中使用图标,我们将使用MaterialButton:

<com.google.android.material.button.MaterialButtonToggleGroupandroid:id="@+id/toggleGroup2"android:layout_width="wrap_content"android:layout_height="wrap_content"app:checkedButton="@id/btnA"app:singleSelection="true"><com.google.android.material.button.MaterialButtonandroid:id="@+id/btnA"style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Android"app:icon="@drawable/ic_android" /><com.google.android.material.button.MaterialButtonandroid:id="@+id/btnS"style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Sunny"app:icon="@drawable/ic_wb_sunny" /></com.google.android.material.button.MaterialButtonToggleGroup>

Android Material Toggle Icon Text

Android Material Toggle图标文字

仅带有图标 (With Icon only)

If we need to use only icons and no text we need to set our custom style. Otherwise, there would be extra padding for text.

如果只需要使用图标而不使用文本,则需要设置自定义样式。 否则,将会有额外的文本填充。

Add the following in styles.xml:

在styles.xml中添加以下内容:

<style name="IconOnlyButton" parent="Widget.MaterialComponents.Button.OutlinedButton"><item name="iconPadding">0dp</item><item name="iconGravity">textStart</item></style>

The code for the Material Button Toggle Group icon only is:

“材质按钮切换组”图标的代码仅是:

<com.google.android.material.button.MaterialButtonToggleGroupandroid:id="@+id/toggleGroup3"android:layout_width="wrap_content"android:layout_height="wrap_content"app:singleSelection="false"><com.google.android.material.button.MaterialButtonstyle="@style/IconOnlyButton"android:layout_width="wrap_content"android:layout_height="wrap_content"app:icon="@drawable/ic_signal_wifi_off"app:iconTint="@android:color/holo_red_light" /><com.google.android.material.button.MaterialButtonstyle="@style/IconOnlyButton"android:layout_width="wrap_content"android:layout_height="wrap_content"app:icon="@drawable/ic_signal_wifi_3_bar_lock" /><com.google.android.material.button.MaterialButtonstyle="@style/IconOnlyButton"android:layout_width="wrap_content"android:layout_height="wrap_content"app:icon="@drawable/ic_wifi" /></com.google.android.material.button.MaterialButtonToggleGroup>

Android Material Toggle Icon Only

仅适用于Android Material Toggle图标

In the above code, we’ve also added iconTint and multi selections.

在上面的代码中,我们还添加了iconTint和多重选择。

设定形状 (Setting Shapes)

To set custom shapes we need to set cornerFamily attribute in a custom style as shown below:

要设置自定义形状,我们需要以自定义样式设置cornerFamily属性,如下所示:

<style name="Cut" parent="ShapeAppearance.MaterialComponents.MediumComponent"><item name="cornerFamily">cut</item><item name="cornerSize">12dp</item></style>

The above style is then set in the shapeAppearance attribute of MaterialButton:

然后在MaterialButton的shapeAppearance属性中设置以上样式:

<com.google.android.material.button.MaterialButtonToggleGroupandroid:id="@+id/toggleGroup4"android:layout_width="wrap_content"android:layout_height="wrap_content"app:singleSelection="false"><com.google.android.material.button.MaterialButtonstyle="@style/IconOnlyButton"android:layout_width="wrap_content"android:layout_height="wrap_content"app:icon="@drawable/ic_signal_wifi_off"app:strokeWidth="2dp"app:shapeAppearance="@style/Cut" /><com.google.android.material.button.MaterialButtonstyle="@style/IconOnlyButton"android:layout_width="wrap_content"android:layout_height="wrap_content"app:strokeWidth="2dp"app:icon="@drawable/ic_signal_wifi_3_bar_lock" /><com.google.android.material.button.MaterialButtonstyle="@style/IconOnlyButton"android:layout_width="wrap_content"android:layout_height="wrap_content"app:icon="@drawable/ic_wifi"app:strokeWidth="2dp"app:shapeAppearance="@style/Cut" /></com.google.android.material.button.MaterialButtonToggleGroup>

In the above code, we’ve also set a strokeWidth on the outline button.

在上面的代码中,我们还在大纲按钮上设置了strokeWidth

Android Material Toggle Button Shape Cut

Android Material Toggle按钮形状切割

圆角形状 (Shape Rounded)

We can set a custom rounded shape with corner radius defined as shown below:

我们可以设置自定义的圆角形状,其圆角半径如下所示:

<style name="Rounded" parent="ShapeAppearance.MaterialComponents.SmallComponent"><item name="cornerFamily">rounded</item><item name="cornerSize">16dp</item></style>
<com.google.android.material.button.MaterialButtonToggleGroupandroid:id="@+id/toggleGroup5"android:layout_width="wrap_content"android:layout_height="wrap_content"app:singleSelection="false"><com.google.android.material.button.MaterialButtonstyle="@style/IconOnlyButton"android:layout_width="wrap_content"android:layout_height="wrap_content"app:icon="@drawable/ic_signal_wifi_off"app:shapeAppearance="@style/Rounded"app:strokeColor="@android:color/black"app:strokeWidth="4dp" /><com.google.android.material.button.MaterialButtonstyle="@style/IconOnlyButton"android:layout_width="wrap_content"android:layout_height="wrap_content"app:icon="@drawable/ic_signal_wifi_3_bar_lock"app:strokeColor="@android:color/black"app:strokeWidth="4dp" /><com.google.android.material.button.MaterialButtonstyle="@style/IconOnlyButton"android:layout_width="wrap_content"android:layout_height="wrap_content"app:icon="@drawable/ic_wifi"app:shapeAppearance="@style/Rounded"app:strokeColor="@android:color/black"app:strokeWidth="4dp" /></com.google.android.material.button.MaterialButtonToggleGroup>

Android Material Toggle Button Shape Rounded

Android材质切换按钮形状四舍五入

与版式 (With Typography)

The following style adds a font family on the text of the MaterialButtons:

以下样式在MaterialButtons的文本上添加了字体系列:

<style name="CustomFont" parent="TextAppearance.MaterialComponents.Headline4"><item name="fontFamily">@font/amatic</item><item name="android:textAllCaps">false</item></style>
<com.google.android.material.button.MaterialButtonToggleGroupandroid:id="@+id/toggleGroup6"android:layout_width="wrap_content"android:layout_height="wrap_content"app:singleSelection="false"><com.google.android.material.button.MaterialButtonstyle="@style/Widget.MaterialComponents.Button.OutlinedButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Java"android:textAppearance="@style/CustomFont"app:strokeColor="@android:color/black" /><com.google.android.material.button.MaterialButtonstyle="@style/Widget.MaterialComponents.Button.OutlinedButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Python"android:textAppearance="@style/CustomFont"app:strokeColor="@android:color/black" /><com.google.android.material.button.MaterialButtonstyle="@style/Widget.MaterialComponents.Button.OutlinedButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="C++"android:textAppearance="@style/CustomFont"app:strokeColor="@android:color/black" /></com.google.android.material.button.MaterialButtonToggleGroup>

We’ve already added a custom font file in the font folder under res.

我们已经在res下的font文件夹中添加了自定义字体文件。

MaterialButtonToggleGroup has an addOnButtonCheckedListener callback which gets triggered when a button is checked.

MaterialButtonToggleGroup具有addOnButtonCheckedListener回调,该回调在选中按钮时触发。

That brings an end to this tutorial. You can download the source code containing all the above customizations from the links below:

这样就结束了本教程。 您可以从下面的链接下载包含上述所有自定义项的源代码:

AndroidMaterialButtonToggleGroupAndroidMaterialButtonToggleGroup
Github Project LinkGithub项目链接

翻译自: https://www.journaldev.com/31950/android-materialbuttontogglegroup

Android MaterialButtonToggleGroup相关推荐

  1. Kotlin第七章: Android四大组件

    1. 四大组件 四大组件是每一个Android人必须要会,要掌握的知识点,因为他们是我们在日常开发工作中打交道最频繁的组件,而且他们四个在不同的领域扮演着极其重要的角色. Activity: 负责用户 ...

  2. Unity5.6+ 导出Android 应用程序apk的环境配置及导出过程

    首先下载并安装安卓SDK和java的JDK 安卓sdk下载: http://www.android-studio.org/ 也可以在这下载: 链接:http://pan.baidu.com/s/1bp ...

  3. Android 的NDK的Makefile编写

    Android.mk 是google根据Linux GNU Makefile精简编译脚本.具体来说:这就是GNU Makefile的一小部分. 举一个简单例子: LOCAL_PATH := $(cal ...

  4. Android Animation (安卓动画)概念简介

    Android Animation Android 四种动画分别为逐帧动画和补间动画.属性动画.过渡动画: Frame Animation (逐帧动画) 实现方式:xml 和 Java代码 图片跳转的 ...

  5. 基于Android和SpringBoot的购物App

    (Shopping)购物应用商城 本软件使用Android和SpringBoot.JavaWeb技术实现:并结合百度LBS平台的SDK.支付宝App支付客户端SDK.MobTech的ShareSDK: ...

  6. Android数据持久化:SharePreference

    SharePreference:作为Android数据持久化的一种,具有一定的便捷性,适合存储一些体积小的数据. 存储数据方式:键值对的方式,类似于Map: 利用SharePreference.Edi ...

  7. Android数据持久化:文件存储

    数据持久化: 数据可分为瞬时数据和关键数据.保存在内存之中的数据是瞬时数据,而对于一些关键性数据,后期需要持续使用的,应当保存在存储设备中: 持久化保存方式: 文件存储.SharePreference ...

  8. Android Studio中RecycerView依赖库加载问题

    依赖包导入思考: 参考资料:recycleview导包问题 打开修改本项目中的build.gradle; 切勿着急添加包,应当提前查看其中的版本号(因为加载的v7包要和其版本保持一致性): 例如: 因 ...

  9. Android布局优化之include、merge、ViewStub

    include:引入重复使用的相同布局 merge:减少include布局的层级,将子元素直接添加到merge标签的parent中 ViewStub:其实就是一个宽高都为0的一个View,它默认是不可 ...

最新文章

  1. 关于Windows不能在本地计算机启动Apache2.......并参考特定服务错误代码1问题解决...
  2. vc的速度有c语言快吗,大家帮看看,怎么回事?Delphi竟比vc++还快
  3. 《快活帮》第九次团队作业:Beta冲刺与验收准备
  4. Block 的循环引用
  5. urllib2.HTTPError: HTTP Error 403: Forbidden的解决方案
  6. Android Realm相关操作
  7. 初始化稀疏矩阵 matlab,访问稀疏矩阵 - MATLAB Simulink - MathWorks 中国
  8. android 动态生成fragment,Android动态加载fragment(fragment复用)
  9. 《统一沟通-微软-实战》-6-部署-2-中介服务器-5-语音路由-语音策略
  10. 面面俱到,这 23 个公共数据集赶紧Mark起来!
  11. spring boot2 整合(三)JOOQ工具
  12. OpenCV-图像处理(05、图像混合)
  13. 扫码点餐小程序有哪些优势
  14. 腾讯云短信API调用
  15. 音乐节拍自动标记插件 BeatEdit
  16. 绘制地图其实并不难!如何绘制地图?看看Smartbi的制作方法
  17. 用python做乘法口诀表_如何用python编写乘法口诀表
  18. Java之下载word文档,linux视频监控
  19. 计算机报名怎么老是密码错误,中考报名显示密码错误 中考网上报名说我密码错误怎么办...
  20. 程序员创业者有哪些优劣势

热门文章

  1. IP通信基础 3.21
  2. Ubuntu18.0.4配置Hadoop1.2.1环境
  3. opencv3 学习三 - 图像输入输出显示等
  4. 02: python3使用email和smtplib库发送邮件
  5. Input禁用文本框
  6. 桥接模式和php实现
  7. Lnixu Bash
  8. [cpp] 重载运算符规律总结
  9. Linux C编程之流操作fopen函数的mode
  10. 控制变频器调节电机梯形图_如何通过PLC控制变频器进行电机转速控制,含代码...