Android onTouchEvent的Force RelativeLayout焦点
我正在开发我的第一个游戏。机制很简单:在屏幕上随机移动的泡泡。我的画布是相对活跃的。布局很简单Android onTouchEvent的Force RelativeLayout焦点,android,touch,android-relativelayout,Android,Touch,Android Relativelayout,我正在开发我的第一个游戏。机制很简单:在屏幕上随机移动的泡泡。我的画布是相对活跃的。布局很简单 <RelativeLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/header" /> 问题是,有时触摸事件不会发生。我想知道是否有某种机制可以始
<RelativeLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/header" />
问题是,有时触摸事件不会发生。我想知道是否有某种机制可以始终抓住焦点,确保我的相对物中的触摸事件
如果您想捕获所有
RelativeLayout
的TouchEvent
事件,欢迎提供任何帮助,然后实现自定义视图扩展RelativeLayout
并在onInterceptTouchEvent()中处理事件
例如:
public class RelativeLayoutExt extends RelativeLayout {
public RelativeLayoutExt(Context context) {
super(context);
}
public RelativeLayoutExt(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RelativeLayoutExt(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RelativeLayoutExt(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO handle your touches here
return super.onInterceptTouchEvent(ev);
}
}
public class RelativeLayoutExt extends RelativeLayout {
public RelativeLayoutExt(Context context) {
super(context);
}
public RelativeLayoutExt(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RelativeLayoutExt(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public RelativeLayoutExt(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO handle your touches here
return super.onInterceptTouchEvent(ev);
}
}