material

您所在的位置:网站首页 小说顾盼生辉只为你相思意 material

material

#material| 来源: 网络整理| 查看: 265

Buttons

Buttons allow users to take actions, and make choices, with a single tap.

Contents

Using buttons Elevated button Filled button Filled tonal button Outlined button Text button Toggle button Icon button Theming Using buttons

Before you can use Material buttons, you need to add a dependency to the Material Components for Android library. For more information, go to the Getting started page.

Note: is auto-inflated as via MaterialComponentsViewInflater when using a Theme.Material3.* theme.

Making buttons accessible

Buttons support content labeling for accessibility and are readable by most screen readers, such as TalkBack. Text rendered in buttons is automatically provided to accessibility services. Additional content labels are usually unnecessary.

For more information on content labels, go to the Android accessibility help guide.

Types

There are five types of common buttons: 1. Elevated button, 2. Filled button, 3. Filled tonal button, 4. Outlined button, 5. Text button.

Toggle button is an additional pattern using a segmented container or icon.

Elevated button

Elevated buttons are essentially outlined buttons with a shadow. To prevent shadow creep, only use them when absolutely necessary, such as when the button requires visual separation from a patterned background.

Elevated button examples

API and source code:

MaterialButton Class description Class source

The following example shows an elevated button with a text label.

In the layout:

In code:

elevatedButton.setOnClickListener { // Respond to button press } Adding an icon to an elevated button

The following example shows an elevated button with an icon.

In the layout:

Anatomy and key properties

An elevated button has a text label, a stroked container and an optional icon.

Container Label text Icon Text label attributes Element Attribute Related method(s) Default value Text label android:text setTextgetText null Color android:textColor setTextColorgetTextColor ?attr/colorOnSurface (see all states) Typography android:textAppearance setTextAppearance ?attr/textAppearanceLabelLarge Container attributes Element Attribute Related method(s) Default value Color app:backgroundTint setBackgroundColorsetBackgroundTintListgetBackgroundTintList ?attr/colorSurface (see all states) Stroke color app:strokeColor setStrokeColorsetStrokeColorResourcegetStrokeColor null Stroke width app:strokeWidth setStrokeWidthsetStrokeWidthResourcegetStrokeWidth 0dp Shape app:shapeAppearance setShapeAppearanceModelgetShapeAppearanceModel ?attr/shapeAppearanceSmallComponent Elevation app:elevation setElevationgetElevation 1dp Ripple color app:rippleColor setRippleColorsetRippleColorResourcegetRippleColor ?attr/colorOnSurface at 16% opacity (see all states) Icon attributes Element Attribute Related method(s) Default value Icon app:icon setIconsetIconResourcegetIcon null Color app:iconTint setIconTintsetIconTintResourcegetIconTint ?attr/colorOnSurface (see all states) Size app:iconSize setIconSizegetIconSize wrap_content Gravity (position relative to text label) app:iconGravity setIconGravitygetIconGravity start Padding (space between icon and text label) app:iconPadding setIconPaddinggetIconPadding 8dp Styles Element Style Default style Widget.Material3.Button.ElevatedButton Icon style Widget.Material3.Button.ElevatedButton.Icon

See the full list of styles and attrs.

Filled button

Filled button's contrasting surface color makes it the most prominent button after the FAB. It’s used for final or unblocking actions in a flow.

Note The filled button is the default style if the style is not set.

Filled button examples

API and source code:

MaterialButton Class description Class source

The following example shows a filled button with a text label and a filled container.

In the layout:

Note: Since this is the default type, you don't need to specify a style tag as long as you are using a Material Components Theme. If not, set the style to @style/Widget.Material3.Button.

In code:

filledButton.setOnClickListener { // Respond to button press } Adding an icon to a filled button

The following example shows a filled button with an icon.

In the layout:

Anatomy and key properties

A filled button has a text label, a filled container and an optional icon.

Container Label text Icon Text label attributes Element Attribute Related method(s) Default value Text label android:text setTextgetText null Color android:textColor setTextColorgetTextColor ?attr/colorOnPrimary (see all states) Typography android:textAppearance setTextAppearance ?attr/textAppearanceLabelLarge Container attributes Element Attribute Related method(s) Default value Color app:backgroundTint setBackgroundColorsetBackgroundTintListgetBackgroundTintList ?attr/colorPrimary (see all states) Stroke color app:strokeColor setStrokeColorsetStrokeColorResourcegetStrokeColor null Stroke width app:strokeWidth setStrokeWidthsetStrokeWidthResourcegetStrokeWidth 0dp Shape app:shapeAppearance setShapeAppearanceModelgetShapeAppearanceModel ?attr/shapeAppearanceSmallComponent Elevation app:elevation setElevationgetElevation 2dp Ripple color app:rippleColor setRippleColorsetRippleColorResourcegetRippleColor ?attr/colorOnPrimary at 16% opacity (see all states) Icon attributes Element Attribute Related method(s) Default value Icon app:icon setIconsetIconResourcegetIcon null Color app:iconTint setIconTintsetIconTintResourcegetIconTint ?attr/colorOnPrimary (see all states) Size app:iconSize setIconSizegetIconSize wrap_content Gravity (position relative to text label) app:iconGravity setIconGravitygetIconGravity start Padding (space between icon and text label) app:iconPadding setIconPaddinggetIconPadding 8dp Styles Element Style Default style Widget.Material3.Button Icon style Widget.Material3.Button.Icon Unelevated style Widget.Material3.Button.UnelevatedButton Unelevated icon style Widget.Material3.Button.UnelevatedButton.Icon

Default style theme attribute: ?attr/materialButtonStyle

See the full list of styles and attrs.

Filled tonal button

Filled tonal buttons have a lighter background color and darker label color, making them less visually prominent than a regular filled button. They’re still used for final or unblocking actions in a flow, but may be better when these actions don’t require quite so much emphasis.

Filled tonal button examples

API and source code:

MaterialButton Class description Class source

The following example shows a filled tonal button with a text label and a filled container.

In the layout:

In code:

filledTonalButton.setOnClickListener { // Respond to button press } Adding an icon to a filled tonal button

The following example shows a filled tonal button with an icon.

In the layout:

Anatomy and key properties

A filled tonal button has a text label, a filled container and an optional icon.

Container Label text Icon Text label attributes Element Attribute Related method(s) Default value Text label android:text setTextgetText null Color android:textColor setTextColorgetTextColor ?attr/colorOnSecondaryContainer (see all states) Typography android:textAppearance setTextAppearance ?attr/textAppearanceLabelLarge Container attributes Element Attribute Related method(s) Default value Color app:backgroundTint setBackgroundColorsetBackgroundTintListgetBackgroundTintList ?attr/colorSecondaryContainer (see all states) Stroke color app:strokeColor setStrokeColorsetStrokeColorResourcegetStrokeColor null Stroke width app:strokeWidth setStrokeWidthsetStrokeWidthResourcegetStrokeWidth 0dp Shape app:shapeAppearance setShapeAppearanceModelgetShapeAppearanceModel ?attr/shapeAppearanceSmallComponent Elevation app:elevation setElevationgetElevation 2dp Ripple color app:rippleColor setRippleColorsetRippleColorResourcegetRippleColor ?attr/colorOnSecondaryContainer at 16% opacity (see all states) Icon attributes Element Attribute Related method(s) Default value Icon app:icon setIconsetIconResourcegetIcon null Color app:iconTint setIconTintsetIconTintResourcegetIconTint ?attr/colorOnSecondaryContainer (see all states) Size app:iconSize setIconSizegetIconSize wrap_content Gravity (position relative to text label) app:iconGravity setIconGravitygetIconGravity start Padding (space between icon and text label) app:iconPadding setIconPaddinggetIconPadding 8dp Styles Element Style Default style Widget.Material3.Button.TonalButton Icon style Widget.Material3.Button.TonalButton.Icon

See the full list of styles and attrs.

Outlined button

Outlined buttons are for actions that need attention but aren’t the primary action, such as “See all” or “Add to cart.” This is also the button used to give someone the opportunity to change their mind or escape a flow.

Outlined button examples

API and source code:

MaterialButton Class description Class source

The following example shows an outlined button with a text label and stroked container.

In the layout:

In code:

outlinedButton.setOnClickListener { // Respond to button press } Adding an icon to an outlined button

The following example shows an outlined button with an icon.

In the layout:

Anatomy and key properties

An outlined button has a text label, a stroked container and an optional icon.

Container Label text Icon Text label attributes Element Attribute Related method(s) Default value Text label android:text setTextgetText null Color android:textColor setTextColorgetTextColor ?attr/colorOnSurface (see all states) Typography android:textAppearance setTextAppearance ?attr/textAppearanceLabelLarge Container attributes Element Attribute Related method(s) Default value Color app:backgroundTint setBackgroundColorsetBackgroundTintListgetBackgroundTintList @android:color/transparent (see all states) Stroke color app:strokeColor setStrokeColorsetStrokeColorResourcegetStrokeColor ?attr/colorOnSurface at 12% opacity (see all states) Stroke width app:strokeWidth setStrokeWidthsetStrokeWidthResourcegetStrokeWidth 1dp Shape app:shapeAppearance setShapeAppearanceModelgetShapeAppearanceModel ?attr/shapeAppearanceSmallComponent Elevation app:elevation setElevationgetElevation 0dp Ripple color app:rippleColor setRippleColorsetRippleColorResourcegetRippleColor ?attr/colorOnSurface at 16% opacity (see all states) Icon attributes Element Attribute Related method(s) Default value Icon app:icon setIconsetIconResourcegetIcon null Color app:iconTint setIconTintsetIconTintResourcegetIconTint ?attr/colorOnSurface (see all states) Size app:iconSize setIconSizegetIconSize wrap_content Gravity (position relative to text label) app:iconGravity setIconGravitygetIconGravity start Padding (space between icon and text label) app:iconPadding setIconPaddinggetIconPadding 8dp Styles Element Style Default style Widget.Material3.Button.OutlinedButton Icon style Widget.Material3.Button.OutlinedButton.Icon

Default style theme attribute: ?attr/materialButtonOutlinedStyle

See the full list of styles and attrs.

Text button

Text buttons have less visual prominence, so should be used for low emphasis actions, such as when presenting multiple options.

Text button examples

API and source code:

MaterialButton Class description Class source

The following example shows a text button with a text label.

In the layout:

In code:

textButton.setOnClickListener { // Respond to button press } Adding an icon to a text button

The following example shows a text button with an icon.

In the layout:

Anatomy and key properties

A text button has a text label, a transparent container and an optional icon.

Label text Icon Text label attributes Element Attribute Related method(s) Default value Text label android:text setTextgetText null Color android:textColor setTextColorgetTextColor ?attr/colorOnSurface (see all states) Typography android:textAppearance setTextAppearance ?attr/textAppearanceLabelLarge Container attributes Element Attribute Related method(s) Default value Color app:backgroundTint setBackgroundColorsetBackgroundTintListgetBackgroundTintList @android:color/transparent (see all states) Stroke color app:strokeColor setStrokeColorsetStrokeColorResourcegetStrokeColor null Stroke width app:strokeWidth setStrokeWidthsetStrokeWidthResourcegetStrokeWidth 0dp Shape app:shapeAppearance setShapeAppearanceModelgetShapeAppearanceModel ?attr/shapeAppearanceSmallComponent Elevation app:elevation setElevationgetElevation 0dp Ripple color app:rippleColor setRippleColorsetRippleColorResourcegetRippleColor ?attr/colorOnSurface at 16% opacity (see all states) Icon attributes Element Attribute Related method(s) Default value Icon app:icon setIconsetIconResourcegetIcon null Color app:iconTint setIconTintsetIconTintResourcegetIconTint ?attr/colorOnSurface (see all states) Size app:iconSize setIconSizegetIconSize wrap_content Gravity (position relative to text label) app:iconGravity setIconGravitygetIconGravity start Padding (space between icon and text label) app:iconPadding setIconPaddinggetIconPadding 8dp Styles Element Style Default style Widget.Material3.Button.TextButton Icon style Widget.Material3.Button.TextButton.Icon Full Width Buttons Widget.Material3.Button.TextButton.Dialog.FullWidth

Default style theme attribute: ?attr/borderlessButtonStyle

See the full list of styles and attrs.

Toggle button

Toggle buttons can be used to select from a group of choices.

There are two types of toggle buttons:

Toggle button Icon Toggle button

To emphasize groups of related toggle buttons, a group should share a common container.

Toggle button examples

API and source code:

MaterialButtonToggleGroup Class description Class source MaterialButton Class description Class source

The following example shows a toggle button with three buttons that have text labels.

In the layout:

In code:

toggleButton.addOnButtonCheckedListener { toggleButton, checkedId, isChecked -> // Respond to button selection } Implementing an icon-only toggle button

The following example shows a toggle button with three buttons that have icons.

In res/values/styles.xml:

0dp 0dp 0dp 12dp 12dp 48dp 48dp

In the layout:

Anatomy and key properties

A toggle button has a shared stroked container, icons and/or text labels.

Container Icon Selection attributes Element Attribute Related method(s) Default value Single selection app:singleSelection setSingleSelectionisSingleSelection false Selection required app:selectionRequired setSelectionRequiredisSelectionRequired false **Enable the group and all children android:enabled setEnabledisEnabled true Styles Element Style Default style Widget.Material3.MaterialButtonToggleGroup

Default style theme attribute: ?attr/materialButtonToggleGroupStyle

See the full list of styles and attrs.

Icon

Icons can be used as toggle buttons when they allow selection, or deselection, of a single choice, such as marking an item as a favorite.

Icon example

API and source code:

CheckBox Class description

Note The CheckBox API is just one of several inputs that can implement the icon button. See other selection controls for more details.

The following example shows an icon that can be used independently or in items of a RecyclerView.

In the layout:

In res/drawable/sl_favourite_24dp.xml:

In code:

icon.setOnCheckedChangeListener { checkBox, isChecked -> // Respond to icon toggle } Icon button

Icon buttons help users take supplementary actions with a single tap. There are two types of icon buttons: standard and contained.

Standard icon button: By default icon buttons will not have a container. Contained icon button: Optionally, it is possible to have a container around the icon.

See Icon button examples section below for more information.

Usage Icon buttons should be used when a compact button is required, such as in a toolbar. Icon buttons can take the form of a wide range of system icons. Ensure the meaning of the icon is unambiguous. Ensure that the tooltip describes the button’s action, rather than the icon. Icon button examples

API and source code:

MaterialButton Class description Class source Standard Icon button

The following example shows a standard icon button.

In the layout:

In code:

iconButton.addOnButtonCheckedListener { iconButton, checkedId, isChecked -> // Respond to button selection } Filled Icon button

The following example shows a contained icon button that is filled.

In the layout:

Filled Tonal Icon button

The following example shows a tonal icon button.

In the layout:

Outlined Icon button

The following example shows an outlined icon button.

In the layout:

Styles & Theme attributes Element Style Theme Attribute Default style Widget.Material3.Button.IconButton ?attr/materialIconButtonStyle Filled Icon Button Widget.Material3.Button.IconButton.Filled ?attr/materialIconButtonFilledStyle Filled Tonal Icon Button Widget.Material3.Button.IconButton.Filled.Tonal ?attr/materialIconButtonFilledTonalStyle Outlined Icon Button Widget.Material3.Button.IconButton.Outlined ?attr/materialIconButtonOutlinedStyle

See the full list of styles and attrs.

Theming buttons

Buttons support Material Theming and can be customized in terms of color, typography and shape.

Button theming example

API and source code:

MaterialButton Class description Class source

The following example shows text, outlined and filled button types with Material Theming.

Implementing button theming

Use theme attributes and styles in res/values/styles.xml to add the theme to all buttons. This affects other components:

... @color/shrine_pink_100 @color/shrine_pink_900 @style/TextAppearance.App.Button @style/ShapeAppearance.App.SmallComponent @font/rubik @font/rubik cut 4dp

Use default style theme attributes, styles and theme overlays. This adds the theme to all buttons but does not affect other components:

... @style/Widget.App.Button.TextButton @style/Widget.App.Button.OutlinedButton @style/Widget.App.Button @style/ThemeOverlay.App.Button.TextButton @style/TextAppearance.App.Button @style/ShapeAppearance.App.SmallComponent @style/ThemeOverlay.App.Button.TextButton @style/TextAppearance.App.Button @style/ShapeAppearance.App.SmallComponent @style/ThemeOverlay.App.Button @style/TextAppearance.App.Button @style/ShapeAppearance.App.SmallComponent @color/shrine_pink_900 @color/shrine_pink_100 @color/shrine_pink_900

Use one of the styles in the layout. That will affect only this button:



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3