2016-07-25 3 views
2

Gibt es eine Möglichkeit, das Touch-Ziel eines TextView zu erhöhen, ohne die Größe des TextView zu erweitern und das Layout zu beeinflussen?Touch-Ziel von TextView erhöhen

Zum Beispiel: Wenn ich Adresseninformationen in einer vertikalen LinearLayout mit mehreren TextView s habe. Ich mache die Telefonnummer TextView anklickbar. Gibt es trotzdem die Telefonnummer TextView Ziel größer zu berühren, ohne eine Polsterung zu schaffen und die umliegenden TextView s weiter weg zu schieben?

+0

Interessante Frage. Ich kenne einen hacky Weg, es zu tun. Überschreiben Sie onTouch und berechnen Sie die Position der Berührung und sehen Sie, ob sie nahe genug an der textView ist und rufen Sie sie dann manuell an click event :) – Vucko

Antwort

0

Sie es mit einem anderen Layout

wickeln kann

So etwas wie dies, können Sie die OnClickListener auf das Layout festgelegt. Wenn Sie das Touch-Ziel vergrößern möchten, erhöhen Sie einfach die Höhe.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="some text"/> 
    </LinearLayout> 

</LinearLayout> 
-1

Wenn Sie die genaue Größe von Textviews wissen Sie FrameLayout as parent für die Textviews haben:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="lorem ipsum" /> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="lorem ipsum" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="8dp" 
      android:layout_marginTop="8dp" 
      android:paddingBottom="8dp" 
      android:paddingTop="8dp" 
      android:text="Phone number" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      android:text="lorem ipsum" /> 
    </FrameLayout> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="lorem ipsum" /> 
</LinearLayout> 

Im Ergebnis können Sie den Touch-Bereich für das Telefon Textview sehen

screenshot of the rendered layout above

Oder Sie können das gleiche Ergebnis erhalten, wenn Sie negative Ränder verwenden

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="lorem ipsum" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="lorem ipsum" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="-8dp" 
     android:layout_marginTop="-8dp" 
     android:paddingBottom="8dp" 
     android:paddingTop="8dp" 
     android:text="Phone number" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:text="lorem ipsum" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="lorem ipsum" /> 
</LinearLayout> 
Verwandte Themen