Android 窗口管理器布局膨胀器没有膨胀全屏布局

标签 android android-layout android-windowmanager

我一直在尝试学习 Android Java 恶作剧,所以对它还很陌生。我设法让一个 Service 工作,该服务可以膨胀布局以创建 TYPE_SYSTEM_OVERLAY。布局是透明的红色,但只占据了屏幕的中间部分。我的希望是让它覆盖并填满整个屏幕。

这就是发生的事情:

enter image description here

主布局 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#0092a3">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activate"
        android:id="@+id/btnActivate"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Exit"
        android:id="@+id/btnExit"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="25dp"
        android:layout_marginBottom="25dp"
        android:width="100dp" />

</RelativeLayout>

覆盖布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#40ff0000">

</LinearLayout>

服务类onCreate方法:

@Override public void onCreate() {
    super.onCreate();

    final WindowManager.LayoutParams params = new  WindowManager.LayoutParams(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
            PixelFormat.TRANSLUCENT);

    windowManager = (WindowManager) getApplicationContext()
            .getSystemService(Context.WINDOW_SERVICE);

    li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    mTopView = (ViewGroup) li.inflate(R.layout.red_overlay, null);

    windowManager.addView(mTopView, params);
}

list :

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        >
        <activity
            android:name=".MainActivity"
            android:theme="@android:style/Theme.Translucent"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <service android:name=".Layer" />

    </application>


    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
</manifest>

一切都很好!我为自己能走到这一步感到非常自豪。可悲的是,我不明白为什么它不会填满屏幕。任何帮助深表感谢。

最佳答案

看来我发帖后解决了我的问题。我一直在谷歌上搜索,发现了这篇有用的文章 heaven.

新的和改进的服务:

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
        PixelFormat.TRANSLUCENT);

windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
mTopView = (LinearLayout) li.inflate(R.layout.red_overlay, null);

windowManager.addView(mTopView, params);

将 ViewGroup 切换为 LinearLayout。

已将 FLAG_FULLSCREEN 切换为 MATCH_PARENT。还有,维奥拉。

关于Android 窗口管理器布局膨胀器没有膨胀全屏布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28613896/

相关文章:

java - 空对象引用上的 OnItemClick.onItemClick(int) 异常?

android - R.styleable 无法解决,为什么?

android - 如何自定义选项卡操作栏

android - 在 PreferenceFragment 中从数据库加载/保存设置

android - 如何制作布局以填充空间直到底部的另一个布局

android - 指定的 child 在膨胀 View 上已经有父错误

android - 如何设置保持屏幕打开和全屏标志?

android - 如何确定状态栏是否在实现您的库的应用程序中是半透明的?

android - 推送通知收不到安卓