2012-10-17 18 views
6

Ho un ToggleButton. Voglio avere più posto per fare clic su questo pulsante. Se aggiungo layout_width, layout_height ecc., L'immagine sembra scadente. Ho anche provato a usare android:padding ma non mi ha aiutato.Come posso aumentare l'area di tocco per i controlli (ToggleButton)?

È necessario per la comodità degli utenti.

enter image description here

+1

qual è il problema se tu stai usando imbottitura? – Syn3sthete

+0

Prova TouchDelegate, ecco un esempio su come usarlo http://stackoverflow.com/questions/1343222/is-there-an-example-of-how-to-use-a-touchdelegate-in-android-to- increase-the-siz/1343796 # 1343796 – ammar26

risposta

3

Soluzione semplice: se si dispone di un'immagine per il pulsante, quindi creare un'area trasparente intorno ad esso (vale a dire per l'area tattile).

L'area grigia può essere trasparente per aumentare l'area di tocco.

enter image description here

Oppure utilizzare TouchDelegate

0

Prendere il margin (luogo di padding) della vostra Button dalla sua parent layout e quindi eseguire opration sul Layout

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

Invece di mettere l'evento di tocco di pulsante di metterlo nel layout contenente il solo il pulsante .. e fissare la dimensione del layout come si desidera

3

Usa TouchDelegate per il tuo ToggleButton come ammar26 ti hanno commentato.

O

Prova questa:

fare della struttura un genitore come LinearLayout o RelativeLayout che coprono il ToggleButton. E ora metti il ​​margine su quel layout del genitore.

Ora, facendo clic su tale layout Genitore, agire per il pulsante di attivazione/disattivazione.

Spero che ti aiuti ad aumentare l'area di tocco per la tua vista.

Happy Coding.

+0

Il tuo secondo approccio è più o meno come funziona TouchDelegate. Il genitore di ToggleButton ascolterà gli eventi di tocco: se uno si trova all'interno dei limiti specificati, il clic verrà inoltrato a ToggleButton da TouchDelegate –

2

aumentare i valori di 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

si può anche aumentare zona tocco impostando 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); 
       } 
      } 
     }); 
    } 
} 
Problemi correlati