我有ToggleButton
。我希望有更多的地方可以点击这个按钮。如果我添加layout_width
,layout_height
等,则图片看起来很糟糕。我也尝试使用android:padding
,但它对我没有帮助。
为了方便用户需要它。
答案 0 :(得分:3)
对TouchDelegate
使用ToggleButton
,因为ammar26已对您进行了评论。
或者
试试这个:
制作涵盖LinearLayout
的{{1}}或RelativeLayout
等父级布局。现在将保证金设置为父布局。
现在,点击该父布局为切换按钮执行操作。
希望它能帮助您增加视图的触控区域。
快乐编码。
答案 1 :(得分:3)
答案 2 :(得分:3)
您还可以通过设置touch delegates
Android Developer Training Blog Post
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the parent view
View parentView = findViewById(R.id.parent_layout);
parentView.post(new Runnable() {
// Post in the parent's message queue to make sure the parent
// lays out its children before you call getHitRect()
@Override
public void run() {
// The bounds for the delegate view (an ImageButton
// in this example)
Rect delegateArea = new Rect();
ImageButton myButton = (ImageButton) findViewById(R.id.button);
myButton.setEnabled(true);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,
"Touch occurred within ImageButton touch region.",
Toast.LENGTH_SHORT).show();
}
});
// The hit rectangle for the ImageButton
myButton.getHitRect(delegateArea);
// Extend the touch area of the ImageButton beyond its bounds
// on the right and bottom.
delegateArea.right += 100;
delegateArea.bottom += 100;
// Instantiate a TouchDelegate.
// "delegateArea" is the bounds in local coordinates of
// the containing view to be mapped to the delegate view.
// "myButton" is the child view that should receive motion
// events.
TouchDelegate touchDelegate = new TouchDelegate(delegateArea,
myButton);
// Sets the TouchDelegate on the parent view, such that touches
// within the touch delegate bounds are routed to the child.
if (View.class.isInstance(myButton.getParent())) {
((View) myButton.getParent()).setTouchDelegate(touchDelegate);
}
}
});
}
}
答案 3 :(得分:2)
不是将触摸事件放在按钮中,而是将其放入仅包含按钮的布局中。并按照您的意愿修复布局的大小
答案 4 :(得分:2)
增加android:padding
:
<SeekBar android:id="@+id/seek" android:layout_width="0dip"
android:layout_height="wrap_content" android:layout_weight="1"
android:paddingTop="5dp" android:paddingBottom="5dp"
android:progressDrawable="@drawable/green_scrubber_progress_horizontal_holo_light"
android:thumb="@drawable/thumb" />
答案 5 :(得分:0)
从margin
获取padding
Button
的{{1}}位置,然后对parent layout
Layout