什么是“触摸目标尺寸"?在Android Material Design中 [英] What exactly is "Touch Target Size" in Android Material Design

查看:49
本文介绍了什么是“触摸目标尺寸"?在Android Material Design中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考Material Design上的Android文档,我经常发现我们被引导将按钮的触摸目标"设置为高度为48dp(

With reference to Android docs on Material Design, I often find that we are guided to set the "touch target" of a button to, let say, 48dp in height (https://www.google.com/design/spec/components/buttons.html#buttons-style). But is it a default value for every Button element, or I have to set it explicitly and how?

推荐答案

如果您对默认的材质按钮"没问题,请使用它.多数情况都可以.Button的材质主题定义为:

If you're fine with the default Material Button, just use it. It's ok for most things. Material theme for Button is defined as:

<!-- Bordered ink button -->
<style name="Widget.Material.Button">
    <item name="background">@drawable/btn_default_material</item>
    <item name="textAppearance">?attr/textAppearanceButton</item>
    <item name="minHeight">48dip</item>
    <item name="minWidth">88dip</item>
    <item name="stateListAnimator">@anim/button_state_list_anim_material</item>
    <item name="focusable">true</item>
    <item name="clickable">true</item>
    <item name="gravity">center_vertical|center_horizontal</item>
</style>

如您所见,minHeight属性设置为48dp,这意味着此按钮的高度至少为48dp.不好的是Button的背景有插图,这意味着通过在按钮周围放置一些透明像素可以实现额外的触摸区域.如此按钮实际上很难正确布局,如以下SO post:

As you can see the minHeight attribute is set to 48dp, which means that this button will be at least 48dp tall. What's bad about it is that Button's background has insets, which means that additional touch area is achieved by having some transparent pixels around a button. It makes buttons really hard to layout properly, as in the following SO post:

如何将Material按钮与其他UI元素对齐

这种小部件设计确实限制了布局构建选项.准则将触摸目标描述为应该影响小部件的外部附加触摸区域.应该这样:

Such widget design really limits layout building options. The guidelines describe a touch target as an external, additional touch area which should affect a widget. It should be done like this:

  • 使用半径为2dp的圆形矩形覆盖默认背景
  • 将minHeight设置为36dp
  • 使用setTouchDelegate更改触摸目标
  • 或覆盖getHitRect更改触摸目标

请参阅:

https://developer.android.com/reference/android/view/TouchDelegate.html

https://developer.android.com/reference/android/view/View.html#getHitRect(android.graphics.Rect)

这篇关于什么是“触摸目标尺寸"?在Android Material Design中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆