【发布时间】:2015-05-27 13:19:22
【问题描述】:
我试图让控制按钮在点击屏幕时弹出/关闭屏幕。他们将覆盖一个视频。我遇到的问题是如何为已膨胀的布局创建触摸事件。
例程如下:
1)给视频设置初始内容;
2) 在视频顶部包含控件的膨胀布局(XML);
3)(需要做的)为新膨胀的布局创建 onTouch 事件,当用户点击屏幕时隐藏/显示按钮。
包含显示/隐藏按钮的布局:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dpad_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:id="@+id/tiltUp"
android:background="@drawable/arrow_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_row="0"
android:layout_column="1"/>
<Button
android:id="@+id/panRight"
android:background="@drawable/arrow_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_row="1"
android:layout_column="2"/>
<Button
android:id="@+id/tiltDown"
android:background="@drawable/arrow_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="8dp"
android:layout_row="2"
android:layout_column="1"/>
<Button
android:id="@+id/panLeft"
android:background="@drawable/arrow_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_row="1"
android:layout_column="0"/>
</GridLayout>
<Button
android:id="@+id/zoomIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="@drawable/zoom_in"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"/>
<Button
android:id="@+id/zoomOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/zoom_out"
android:layout_marginRight="0dp"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/zoomIn"/>
<Button
android:id="@+id/irisDarker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="@drawable/iris_darker"/>
<Button
android:id="@+id/irisBrighter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/irisDarker"
android:background="@drawable/iris_brighter"/>
<Button
android:id="@+id/focusFar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/dpad_grid"
android:layout_marginRight="20dp"
android:layout_alignParentStart="true"
android:background="@drawable/focus_far"/>
<Button
android:id="@+id/focusNear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/focus_near"
android:layout_alignTop="@+id/focusFar"
android:layout_alignStart="@+id/irisBrighter"/>
<Button
android:id="@+id/snapshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/camera"
android:layout_alignEnd="@+id/focusFar"
android:layout_alignParentStart="true"/>
<Button
android:id="@+id/settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/gear"/>
</RelativeLayout>
添加视频和膨胀上述布局的代码:
@Override
public void onResume() {
super.onResume();
RelativeLayout.LayoutParams relativeLayoutParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
mv = new MjpegView(this);
setContentView(mv);
LayoutInflater inflater = getLayoutInflater();
getWindow().addContentView(inflater.inflate(R.layout.screen2, null), relativeLayoutParams);
感谢您的帮助!
编辑:这是我目前正在使用的。为什么我的代码在触摸屏幕时没有进入触摸事件?
LayoutInflater inflater = getLayoutInflater();
control_buttons = inflater.inflate(R.layout.screen2, null);
control_buttons.setOnTouchListener(new View.OnTouchListener() {
boolean gone = true;
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("In OnTouch");
if ( (event.getAction() == MotionEvent.ACTION_DOWN) && (gone == true) ){
getWindow().addContentView(control_buttons, relativeLayoutParams);
gone = false;
return true;
}
else if ((event.getAction() == MotionEvent.ACTION_DOWN) && (gone == false)) {
v.setVisibility(View.GONE);
gone = true;
return true;
}
return false;
}
});
【问题讨论】:
-
触摸事件?你是说 MotionEvents (developer.android.com/reference/android/view/MotionEvent.html)?
-
我认为我不需要像滑动这样的所有动作。我只想在用户点击屏幕时显示/隐藏按钮。
-
你读过 inflater.inflate() 返回的内容吗?
-
我很确定它会返回一个视图,对吧?
-
那么将触摸事件侦听器附加到该视图有什么问题?
标签: java android xml android-layout layout-inflater