如何增加控件(ToggleButton)的触摸面积?

7

我有一个 ToggleButton。我想要这个按钮有更多的点击区域。如果我添加 layout_widthlayout_height 等属性,图片看起来很糟糕。我也尝试使用 android:padding,但没有帮助。

这是为了方便用户。

enter image description here


1
如果您正在使用填充,可能会出现什么问题? - Syn3sthete
尝试使用TouchDelegate,这里有一个如何使用它的示例https://dev59.com/HnM_5IYBdhLWcg3whznx#1343796 - ammar26
7个回答

3

可以像 ammar26 在评论中建议的那样,使用 TouchDelegate 来为你的 ToggleButton 增加触摸区域。

或者

尝试这个:

创建一个父布局,例如LinearLayoutRelativeLayout,将其覆盖在 ToggleButton 上,并为该父布局添加一些边距(margin)。

现在,在单击此父布局时,执行开关按钮相应的操作即可。

希望这能帮助你增加视图的触摸区域。

祝编码愉快!


你的第二种方法或多或少就是TouchDelegate的工作原理。ToggleButton的父级将监听触摸事件:如果在指定的边界内,则点击将通过TouchDelegate转发到ToggleButton。 - Gil Vegliach

3

简单的解决方法:如果你有按钮图片,就在它周围创建一个透明区域(即触摸区域)。

灰色区域可以是透明的,以增加触摸区域。

enter image description here

或者使用TouchDelegate


3
您可以通过设置 触摸委托 来增加触摸区域。了解更多信息,请参阅Android开发者培训博客文章
public class MainActivity extends Activity {

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get the parent view 
        View parentView = findViewById(R.id.parent_layout);

        parentView.post(new Runnable() {
            // Post in the parent's message queue to make sure the parent 
            // lays out its children before you call getHitRect() 
            @Override 
            public void run() { 
                // The bounds for the delegate view (an ImageButton 
                // in this example) 
                Rect delegateArea = new Rect();
                ImageButton myButton = (ImageButton) findViewById(R.id.button);
                myButton.setEnabled(true);
                myButton.setOnClickListener(new View.OnClickListener() {
                    @Override 
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, 
                                "Touch occurred within ImageButton touch region.",  
                                Toast.LENGTH_SHORT).show();
                    } 
                }); 

                // The hit rectangle for the ImageButton 
                myButton.getHitRect(delegateArea);

                // Extend the touch area of the ImageButton beyond its bounds 
                // on the right and bottom. 
                delegateArea.right += 100;
                delegateArea.bottom += 100;

                // Instantiate a TouchDelegate. 
                // "delegateArea" is the bounds in local coordinates of  
                // the containing view to be mapped to the delegate view. 
                // "myButton" is the child view that should receive motion 
                // events. 
                TouchDelegate touchDelegate = new TouchDelegate(delegateArea, 
                        myButton);

                // Sets the TouchDelegate on the parent view, such that touches  
                // within the touch delegate bounds are routed to the child. 
                if (View.class.isInstance(myButton.getParent())) {
                    ((View) myButton.getParent()).setTouchDelegate(touchDelegate);
                } 
            } 
        }); 
    } 
} 

2
增加 android:padding 的值:
<SeekBar android:id="@+id/seek" android:layout_width="0dip"
android:layout_height="wrap_content" android:layout_weight="1" 
android:paddingTop="5dp" android:paddingBottom="5dp"
android:progressDrawable="@drawable/green_scrubber_progress_horizontal_holo_ligh‌​t"
android:thumb="@drawable/thumb" />

2

不要将触摸事件放在按钮中,而是将其放在仅包含按钮的布局中。并根据您的需要调整布局的大小。


0

父布局中获取你的Buttonmargin(即padding的位置),然后对你的Layout执行操作。

mLayout.setonTouchListener(View.onTouchListener{
     // here your working code 
});

0
你可以使用触摸委托来增加触摸区域。
编写一个通用函数,如下所示:
fun View.increaseHitAreaForViews(vararg viewsAndDetail: Pair<View, Int>) {
            val touchDelegateComposite = TouchDelegateComposite(this)
            post {
                viewsAndDetail.forEach { pair ->
                    val rect = Rect()
                    val view = pair.first
                    view.getHitRect(rect)
                    rect.top -= pair.second
                    rect.left -= pair.second
                    rect.bottom += view.measuredHeight + pair.second
                    rect.right += view.measuredWidth + pair.second
                    touchDelegateComposite.addDelegate(TouchDelegate(rect, view))
                }
                touchDelegate = touchDelegateComposite
            }
        }

并且可以在任何你想要增加点击区域的地方调用它。
view.increaseHitAreaForViews(
  Pair(btn, 40)
)

其中,view是父布局,btn是您想要增加触摸区域的控件。您还可以根据需要调整值40


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接