当前位置: 动力学知识库 > 问答 > 编程问答 >

Android Window Manager Layout Inflater is not inflating a full screen layout

问题描述:

I have been attempting to learn Android Java shenanigans, so still very new to it. I managed to get a Service working that inflates a layout to create a TYPE_SYSTEM_OVERLAY. The layout is a transparent red, but only takes up the middle part of my screen. My hope was to have it cover and fill the entire screen.

This is what happens:

Main Layout 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>

Overlay Layout 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>

Service Class onCreate method:

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

}

Manifest:

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

Everything works great! And I am quite proud of myself for getting this far. Sadly, I cannot figure out why it won't fill the screen. Any help is much appreciated.

网友答案:

It figures after I make a post I solve my problem. I just kept searching google and I found this helpful piece of heaven.

The new and improved Service:

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

Switched ViewGroup to LinearLayout.

Switched FLAG_FULLSCREEN to MATCH_PARENT. And, Viola.

分享给朋友:
您可能感兴趣的文章:
随机阅读: