我試圖讓控制按鈕在屏幕被點擊時彈出開/關屏幕。他們將覆蓋視頻。我遇到的問題是如何爲已膨脹的佈局創建觸摸事件。
例程如下:
1)將初始內容設置爲視頻;
2)在視頻頂部打開包含控件的佈局(XML);
3)(需要做)爲用戶點擊屏幕時隱藏/顯示按鈕的新膨脹佈局創建onTouch事件。如何將onTouch設置爲通過通貨膨脹添加的佈局(XML)?
包含的按鈕佈局顯示/隱藏:
<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;
}
});
touch events?你的意思是MotionEvents(http://developer.android.com/reference/android/view/MotionEvent.html)? – pskink
我不認爲我需要所有的動作像滑動和什麼不。我只想讓按鈕在用戶點擊屏幕時顯示/隱藏。 – shoota
你讀過什麼inflater.inflate()返回? – pskink