【发布时间】:2015-03-03 21:10:59
【问题描述】:
我在 Android 中设置了两个非常简单的视图:
这是我的代码,看看它们是否重叠,但由于某种原因,代码总是返回“不重叠”:
私有 FrameLayout firstView; 私有 FrameLayout secondView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstView = (FrameLayout)findViewById(R.id.numberOne);
secondView = (FrameLayout)findViewById(R.id.numberTwo);
if (containsView(secondView, firstView)) {
Toast.makeText(this, "IS overlapping", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "IS NOT overlapping", Toast.LENGTH_LONG).show();
}
}
public boolean containsView(View firstView, View secondView){
int[] pointA = new int[2];
firstView.getLocationOnScreen(pointA);
Rect rectA = new Rect(pointA[0], pointA[1], pointA[0] + firstView.getWidth(), pointA[1] + firstView.getHeight());
int[] pointB = new int[2];
secondView.getLocationOnScreen(pointB);
Rect rectB = new Rect(pointB[0], pointB[1], pointB[0] + secondView.getWidth(), pointB[1] + secondView.getHeight());
return Rect.intersects(rectA, rectB);
}
【问题讨论】:
标签: android intersection android-framelayout rect