增加按钮的可点击区域
Increase the clickable area of the button
我想增加 button.But 按钮中的图像的可点击区域应保持不变 size.Also 我已将图像设置为背景而不是来源。我该怎么做?
<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
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>
现在,在你的 activity 里面,喜欢 -
LinearLayout backbuttonlayout = (LinearLayout)findViewById(R.id.backbuttonlayout);
并对后退按钮布局执行 setOnClickListener()
- 删除边距,在按钮周围使用填充。
- 用一个 LinearLayout 包围按钮,该布局在按钮周围有填充。
- 将与 Button 相同的 onclick 添加到 LinearLayout。
您可以使用填充。它会将 space 放在视图内(margin 会把它放在外面)。
例如,以下代码将提供 20dp 的可点击区域,但背景将为 10dp。
<Button
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/your_background"
android:padding="10dp" />
使用TouchDelegate
Helper class to handle situations where you want a view to have a larger touch area than its actual view bounds. The view whose touch area is changed is called the delegate view. This class should be used by an ancestor of the delegate. To use a TouchDelegate, first create an instance that specifies the bounds that should be mapped to the delegate and the delegate view itself.
例子
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);
}
}
});
}
}
我想这对你有帮助
我想增加 button.But 按钮中的图像的可点击区域应保持不变 size.Also 我已将图像设置为背景而不是来源。我该怎么做?
<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
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>
现在,在你的 activity 里面,喜欢 -
LinearLayout backbuttonlayout = (LinearLayout)findViewById(R.id.backbuttonlayout);
并对后退按钮布局执行 setOnClickListener()
- 删除边距,在按钮周围使用填充。
- 用一个 LinearLayout 包围按钮,该布局在按钮周围有填充。
- 将与 Button 相同的 onclick 添加到 LinearLayout。
您可以使用填充。它会将 space 放在视图内(margin 会把它放在外面)。
例如,以下代码将提供 20dp 的可点击区域,但背景将为 10dp。
<Button
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/your_background"
android:padding="10dp" />
使用TouchDelegate
Helper class to handle situations where you want a view to have a larger touch area than its actual view bounds. The view whose touch area is changed is called the delegate view. This class should be used by an ancestor of the delegate. To use a TouchDelegate, first create an instance that specifies the bounds that should be mapped to the delegate and the delegate view itself.
例子
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);
}
}
});
}
}
我想这对你有帮助