我有一个带有自定义可绘制对象的按钮。
<Button
android:layout_width="22dip"
android:layout_height="23dip"
android:background="@drawable/triangle" />
drawable 是一个具有透明背景的三角形。
|\
| \
|__\
我发现这个按钮很难点击。首先,它相对较小。其次,透明像素不可点击。我想保持可绘制的大小相同,但将点击区域设为三角形大小的两倍。
_____________
| |
| |\ |
| | \ |
| |__\ |
|____________|
最佳答案
您可以使用 TouchDelegate API。
final View parent = (View) button.getParent(); // button: the view you want to enlarge hit area
parent.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
parent.setTouchDelegate( new TouchDelegate( rect , button));
}
});
关于android - 如何在不缩放背景的情况下增加 Android 按钮的点击区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8176105/