增加按钮的可点击区域

6 android button

我想增加按钮的可点击区域.但是按钮中的图像应该保持相同的大小.我也将图像设置为背景而不是源.我怎么能这样做?

 <Button
        android:id="@+id/backbutton"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/arrow"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"

        android:textColor="@color/title_gray"
        android:textSize="14sp"


        android:visibility="visible" />
Run Code Online (Sandbox Code Playgroud)

Str*_*der 9

使用TouchDelegate

帮助程序类,用于处理您希望视图具有比其实际视图边界更大的触摸区域的情况。触摸区域发生变化的视图称为委托视图。该类应该由委托的祖先使用。要使用 TouchDelegate,首先创建一个实例,该实例指定应映射到委托和委托视图本身的边界。

例子

public class LaunchActivity extends Activity {

   private Button MyButton;

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

       MyButton = (Button)findViewById(R.id.Button1);  //Your button ID
       View parent = findViewById(R.id.layout);        //Your Layout ID
       parent.post(new Runnable() {
            @Override
            public void run() {
                Rect delegateArea = new Rect();
                Button delegate = MyButton;
                delegate.getHitRect(delegateArea);
                delegateArea.top -= 600;           //Choose yourself
                delegateArea.bottom += 600;
                delegateArea.left -= 600;
                delegateArea.right += 600;

                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);
                }
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我想这会对你有所帮助


Nar*_*ngh 5

只需制作按钮的父布局(更大尺寸或可点击的尺寸),然后执行类似的点击事件 -

<LinearLayout
 android:id="@+id/backbuttonlayout"
        android:layout_width="50dp"
        android:layout_height="50dp">
<Button
        android:id="@+id/backbutton"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/arrow"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"

        android:textColor="@color/title_gray"
        android:textSize="14sp"


        android:visibility="visible" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

现在,在您的活动中,请做 -

LinearLayout backbuttonlayout = (LinearLayout)findViewById(R.id.backbuttonlayout);
Run Code Online (Sandbox Code Playgroud)

并在backbuttonlayout上执行setOnClickListener()

  • 这不利于可访问性 (2认同)