【问题标题】:Button in top of SurfaceView when adding to WindowManager添加到 WindowManager 时 SurfaceView 顶部的按钮
【发布时间】:2017-04-14 01:48:10
【问题描述】:

这是我的布局文件:

<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);,但似乎没有任何效果。你有什么建议吗?谢谢。

【问题讨论】:

  • 你可以使用线性布局代替Framelayout
  • @MCZ 我使用线性布局,但表面视图仍然与 Button 重叠。
  • 我提交了一段代码,希望对您有所帮助

标签: android surfaceview android-windowmanager


【解决方案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>

【讨论】:

  • 我看不出有什么不同。
【解决方案2】:

此代码经过测试,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>

活动:

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2011-02-10
    • 2016-11-14
    相关资源
    最近更新 更多