2012-10-17 39 views
6

我有一个ToggleButton。我想要更多的地方点击这个按钮。如果我添加layout_widthlayout_height等图像看起来不好。我也试过使用android:padding,但它没有帮助我。如何增加控件的触摸区域(ToggleButton)?

为方便用户需要。

enter image description here

+1

增加触摸区域是什么,如果问题您正在使用 填充? – Syn3sthete

+0

尝试TouchDelegate,这里是一个示例如何使用它http://stackoverflow.com/questions/1343222/is-there-an-example-of-how-to-use-a-touchdelegate-in-android-to-增加-siz/1343796#1343796 – ammar26

回答

3

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

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

enter image description here

或者使用TouchDelegate

0

parent layout把你Buttonmargin(地方padding),然后在你的Layout

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

执行调度研究而不是把触摸事件按钮把它放在布局包含唯一的按钮..并修复布局的大小,因为你愿意

3

使用TouchDelegate为您的0由于ammar26已评论你,所以。

或者

试试这个:

让父母一方布局像LinearLayoutRelativeLayout覆盖的ToggleButton。现在把裕度放到那个Parent布局。

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

希望它可以帮助您增加触摸面积以适合您的视野。

快乐编码。

+0

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

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" /> 
3

您还可以通过设置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); 
       } 
      } 
     }); 
    } 
}