引用
https://blog.csdn.net/Love667767/article/details/105735008文章来源地址https://www.toymoban.com/news/detail-692299.html
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.App0"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.koala.app0;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private static Context context;
private Button button;
private View imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplication().getApplicationContext();
button = findViewById(R.id.btn1);
imageView = LayoutInflater.from(context).inflate(R.layout.png,null);
button.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener(){
@Override
public void onClick(View v) {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
// layoutParams.format = PixelFormat.RGBA_8888;
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// layoutParams.width = 200;
// layoutParams.height = 200;
// layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
// layoutParams.gravity = Gravity.BOTTOM | Gravity.LEFT;
layoutParams.x = 30;
layoutParams.y = 40;
WindowManager windowManager = getWindowManager();
windowManager.addView(imageView,layoutParams);
}
};
public static Context getContext(){
return context;
}
}
其他选项
WindowManager mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams = new WindowManager.LayoutParams();
View mView = LayoutInflater.from(mContext).inflate(R.layout.pop_message_view, null);
//设置图片格式,效果为背景透明
mLayoutParams.format = PixelFormat.RGBA_8888;
//悬浮窗位置
mLayoutParams.x = 768;
mLayoutParams.y = 100;
//悬浮窗大小
mLayoutParams.width = 1026;
mLayoutParams.height = 400;
//悬浮窗起始位置
mLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
//悬浮窗口类型
mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
mLayoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//悬浮窗动画
mLayoutParams.windowAnimations = R.style.toast_anim_view;
mWindowManager.addView(mView, mLayoutParams);
文章来源:https://www.toymoban.com/news/detail-692299.html
到了这里,关于android WindowManager的简单使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!