2017-11-06 5 views
2

私はAndroidの開発で新たなんだと私は左の[戻る]ボタンのヒット領域を増やしたいLinearLayoutのImageButtonの増加ヒットエリア

<LinearLayout 
      android:id="@+id/buttonsLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingTop="5sp" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginBottom="20dp"> 
      <ImageButton 
       android:id="@+id/backButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:background="@drawable/button_close_animation" 
       android:onClick="closeActivity" 
       android:layout_weight="1"/> 
      <ImageButton 
       android:id="@+id/sendButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:background="@drawable/sendbutton_inactive" 
       android:onClick="onSendMessage" 
       android:layout_weight="1" 
       android:layout_marginLeft="30sp"/> 
     </LinearLayout> 

の内側にグループ化された二つのボタンのヒット領域を増やす必要があります右側に送信ボタンがあります。

私は各ボタンにパディングを追加して試してみたが、画像が引き伸ばされ、私は...また、私はバックボタン

Rect delegateAreaBack = new Rect(); 
     ImageButton delegateBack = (ImageButton) findViewById(R.id.backButton); 
     delegateBack.getHitRect(delegateAreaBack); 
     delegateAreaBack.left -= 2600; 
     TouchDelegate expandedAreaBack = new TouchDelegate(delegateAreaBack, delegateBack); 
     if(View.class.isInstance(delegateBack.getParent())) 
      ((View) delegateBack.getParent()).setTouchDelegate(expandedAreaBack); 

で始まるTouchDelegateを使用しようとしたことを望んでいませんが、ヒットエリアが増加していない...私は間違って何をしていますか?

答えて

0
ImageButton button = (ImageButton) findViewById(R.id.backButton); 
button .post(new Runnable() { 
public void run() { 
     final Rect rect = new Rect(); 
     button.getHitRect(rect); 
     rect.top -= 100; // increase top hit area 
     rect.left -= 100; // increase left hit area 
     rect.bottom += 100; // increase bottom hit area   
     rect.right += 100; // increase right hit area 
     button .setTouchDelegate(new TouchDelegate(rect , button)); 
    } 
}); 
+0

に彼のポストからだけでなく@Striderに行きますか? – Dana

+0

親はボタンです – shravani

+1

同じ問題... :(動作していません – Dana

0

ダナ、LinearLayoutのサイズを大きくする必要があるようです。ここで

は、それはのように見えた方法です。ここでhttps://image.prntscr.com/image/mG6IHwS4Tw25kb_6CY5Tqw.png

私はそれが実験の目的のために200dpする高さを変更することにより、https://image.prntscr.com/image/v8Hl0CrJQ3qwZOQoZMdvLg.pngのように見せていた方法です。私が問題に少し実験するために、この単純なコードを使用して、あなたが気づいた場合、送信ボタンの下にクリック可能な領域があり、その後

<LinearLayout 
     android:id="@+id/buttonsLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="200dp" 
     android:paddingTop="5sp" 
     android:layout_marginTop="152dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginLeft="114dp" 
     android:layout_marginStart="114dp"> 
     <ImageButton 
      android:id="@+id/backButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="@mipmap/ic_launcher" 
      android:onClick="closeActivity" 
      android:layout_weight="1"/> 
     <ImageButton 
      android:id="@+id/sendButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="@mipmap/ic_launcher" 
      android:onClick="onSendMessage" 
      android:layout_weight="1" 
      android:layout_marginLeft="30sp"/> 
</LinearLayout> 

だからすべてここであなたのXMLは次のように見せていたどのようにされました増加した。あなたは私の第二のスクリーンショットに気づけば今、私はあなたがこの仕事をしたい場合は、レイアウトの面積を大きくする必要があることを意味しているので、下側にして上にライナーレイアウトのギャップが増加している

public class MainActivity extends AppCompatActivity { 
ImageButton button1, button2; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    button1 = (ImageButton) findViewById(R.id.backButton); 
    button2 = (ImageButton) findViewById(R.id.sendButton); 


    View parent = findViewById(R.id.root); //Your Layout ID 
    parent.post(new Runnable() { 
     @Override 
     public void run() { 
      Rect delegateArea = new Rect(); 
      ImageButton delegate = button2; 
      delegate.getHitRect(delegateArea); 
      delegateArea.bottom += 260; 


      TouchDelegate expandedArea = new TouchDelegate(delegateArea, delegate); 
      // give the delegate to an ancestor of the view we're 
      // delegating the 
      // area to 
      if (View.class.isInstance(delegate.getParent())) { 
       ((View) delegate.getParent()) 
       .setTouchDelegate(expandedArea); 
      } 
     } 
    }); 

} 

} 

ボタンの周り。

あなたの問題を解決するためにどのような方向性が必要かを試してみるだけで、コードにこれらの変更を加えました。これがあなたを助けることを願っています。

クレジットは、親がボタンの親であるIncrease the clickable area of the button