有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何创建一个透明的Android应用程序,它将占据整个屏幕(位于状态/导航栏上方)?

我想知道,如何使我的应用程序透明并覆盖安卓设备的整个屏幕

这意味着也要在状态/导航栏的顶部!(请不要告诉我将我的应用程序置于“全屏”状态,因为这不是理想的效果,我希望状态栏可见,但“在”我的层/应用程序下,仍然能够处理触摸!)

通过使用以下代码动态创建视图,我可以完成一些工作:

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView= new TextView(this);
        textView.setText("This text is above status bar!");

        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams
        (
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            // Allows the view to be on top of the StatusBar
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            // Keeps the button presses from going to the background window
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
            // Enables the notification to recieve touch events
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
            // Draws over status bar
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
            PixelFormat.TRANSLUCENT
        );

        layoutParams.gravity = Gravity.TOP | Gravity.CENTER;

        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        windowManager.addView(textView, layoutParams);
    }
}

这里textView是动态创建的,然后添加到窗口管理器中,结果是状态栏上方显示的我的文本。到现在为止,一直都还不错!但是如何让它与我的XML布局activity_main.xml一起工作呢

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    tools:context=".MainActivity"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:gravity="center"
    安卓:id="@+id/Root"
>

    <!-- Complete layout here (buttons, textviews...) -->

</RelativeLayout>

到目前为止,我尝试将Root视图添加到窗口管理器时,出现了错误,说我已经有了一个父视图,如下所示:

// Load the activity layout
setContentView(R.layout.main_activity);

// Get the layout root element
root = (RelativeLayout) findViewById(R.id.Root);

WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams
(
    WindowManager.LayoutParams.MATCH_PARENT,
    WindowManager.LayoutParams.MATCH_PARENT,
    // Allows the view to be on top of the StatusBar
    WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
    // Keeps the button presses from going to the background window
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
    // Enables the notification to recieve touch events
    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
    // Draws over status bar
    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
    PixelFormat.TRANSLUCENT
);

layoutParams.gravity = Gravity.TOP | Gravity.CENTER;
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.addView(root, layoutParams);

或者,当我试图使用root.setLayoutParams(layoutParams);直接设置Root元素的布局参数时,它给了我一个错误,说明它无法将WindowManager.LayoutParams转换为RelativeLayout.marginLayoutParams

我被困住了,欢迎你的帮助


共 (3) 个答案

  1. # 1 楼答案

    为布局参数创建一个类型为“应用程序覆盖”的窗口,添加FLAG_NOT_TOUCHABLE/FLAG_NOT_FOCUSABLE以获得良好的测量效果,像素格式。不透明,并将组件的Alpha设置为(0)。然后,您可以关闭应用程序(onBackPressed())并使用该窗口——不过您确实需要相关的权限(SYSTEM_ALERT_window)

  2. # 2 楼答案

    好吧,考虑到我对这件事的所有搜索,我发现我试图实现的(在屏幕上的所有内容上加一层,意味着状态和导航栏),对于操作系统高于KitKat(4.4)的设备来说已经不可能了

    所以我很失望地放弃了这个项目()

  3. # 3 楼答案

    关于父错误,您可以执行以下操作: ((ViewGroup)root.getParent()).removeView(root);