添加到WindowManager时,在SurfaceView顶部按钮

时间:2017-04-13 11:56:43

标签: android surfaceview android-windowmanager

这是我的布局文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:orientation="vertical">

<SurfaceView
    android:id="@+id/mediaView"
    android:layout_width="300dp"
    android:layout_height="200dp"
    android:background="@android:color/transparent" />

<Button
    android:id="@+id/btnClose"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="close" />

</FrameLayout>

我将其添加为System alert windows

  // hiển thị floating view
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    windowManager.addView(abovedFrameLayout, params);

我的问题是,无论我尝试什么,按钮都不会显示。它始终位于SurfaceView之下。我测试过:mSurfaceView.setZOrderMediaOverlay(false); mSurfaceView.setZOrderOnTop(false);,但似乎没有任何效果。你有什么建议吗?感谢。

2 个答案:

答案 0 :(得分:1)

试试这个,你应该使用RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <SurfaceView
        android:id="@+id/mediaView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_gravity="bottom"
        android:padding="20dp">

        <Button
            android:id="@+id/btnClose"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="close" />

    </RelativeLayout>

</FrameLayout>

答案 1 :(得分:1)

此代码已经过测试,而surfaceviewer和botton不重叠 Xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="close" />

    <SurfaceView
        android:id="@+id/mediaView"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:background="@android:color/transparent" />

</LinearLayout>

Acitivy:

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_TOAST,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
        PixelFormat.TRANSLUCENT);

params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

ImageView v = new ImageView(surfaceview.this);
v.setBackgroundResource(R.drawable.nimbledroid);
windowManager.addView(v, params);