增加控件可点击区域
2017-02-10 本文已影响49人
真胖大海
一.使用TouchDelegate增加控件可点击区域
TouchDelegate API
TouchDelegate Demo
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();
}
});
// 获取子视图初始时的区域
myButton.getHitRect(delegateArea);
delegateArea.right += 100;
delegateArea.bottom += 100;
增加后的区域 ,子控件
TouchDelegate touchDelegate = new TouchDelegate(delegateArea,myButton);
if (View.class.isInstance(myButton.getParent())) {
((View) myButton.getParent()).setTouchDelegate(touchDelegate);///父容器设置代理
}
}
});
}
}
二.使用Padding
增加padding语气变大