Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > android WindowManager使用

android WindowManager的简单使用实例详解

作者:玲珑·

这篇文章主要介绍了android WindowManager的简单使用,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

android WindowManager的简单使用

<?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;
    }
}

到此这篇关于android WindowManager的简单使用的文章就介绍到这了,更多相关android WindowManager使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文