【问题标题】:How I can increase the touch area for controls (ToggleButton)?如何增加控件的触摸区域(ToggleButton)?
【发布时间】:2012-10-17 07:18:36
【问题描述】:

我有一个ToggleButton。我想有更多的地方点击这个按钮。如果我添加layout_widthlayout_height 等,图像看起来很糟糕。我也尝试过使用android:padding,但对我没有帮助。

为了方便用户需要。

【问题讨论】:

标签: android android-layout android-widget


【解决方案1】:

简单的解决方案:如果您有按钮的图像,则在其周围创建透明区域(即用于触摸区域)。

灰色区域可以透明,增加触控面积。

或使用TouchDelegate

【讨论】:

    【解决方案2】:

    你也可以通过设置touch delegatesAndroid Developer Training Blog Post来增加触摸面积

    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);
                    } 
                } 
            }); 
        } 
    } 
    

    【讨论】:

      【解决方案3】:

      使用 TouchDelegate 作为您的 ToggleButton,因为 ammar26 已评论您。

      或者

      试试这个:

      制作一个覆盖ToggleButton 的父布局,如LinearLayoutRelativeLayout。现在为该父布局添加边距。

      现在,单击该父布局时,对切换按钮执行操作。

      希望它能帮助您增加视图的触摸区域。

      快乐编码。

      【讨论】:

      • 您的第二种方法或多或少是 TouchDelegate 的工作方式。 ToggleButton 的父级将监听触摸事件:如果一个在指定的边界内,点击将被 TouchDelegate 转发到 ToggleButton
      【解决方案4】:

      不要将触摸事件放在按钮中,而是将其放在包含唯一按钮的布局中..并根据您的意愿固定布局的大小

      【讨论】:

        【解决方案5】:

        增加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" />
        

        【讨论】:

          【解决方案6】:

          parent layout 中取出Buttonmargin(代替padding),然后对Layout 执行操作

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-01-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-08-17
            • 2019-12-09
            相关资源
            最近更新 更多