一、悬浮常驻文本遮挡问题复现

1、Service
  • FloatingTextViewService.java
public class FloatingTextViewService extends Service {

    private TextView tvFloating;
    private WindowManager windowManager;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

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

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        // 创建布局参数
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
                        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY :
                        WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );

        // 设置位置为左下角
        params.gravity = Gravity.BOTTOM | Gravity.START;
        params.x = 0;
        params.y = 0;

        LayoutInflater inflater = LayoutInflater.from(this);
        tvFloating = (TextView) inflater.inflate(R.layout.floating_text_view, null);
        tvFloating.setText("test content");

        // 添加到窗口
        windowManager.addView(tvFloating, params);
    }

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

        if (tvFloating != null) {
            windowManager.removeView(tvFloating);
        }
    }
}
  • floating_text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_floating"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="12dp"
    android:textSize="14sp" />
2、Manifest
  • AndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<application>

    ...

    <service
        android:name=".service.FloatingTextViewService"
        android:enabled="true"
        android:exported="false" />
</application>
3、Activity Layout
  • activity_floating_text_view_service.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FloatingTextViewServiceActivity">

    <Button
        android:id="@+id/btn_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
4、Activity Code
  • FloatingTextViewServiceActivity.java
public class FloatingTextViewServiceActivity extends AppCompatActivity {

    private static final String TAG = FloatingTextViewServiceActivity.class.getSimpleName();

    private ActivityResultLauncher<Intent> overlayPermissionLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_floating_text_view_service);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        test();
    }

    private void test() {
        overlayPermissionLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (checkOverlayPermission()) {
                        createFloatingTextView();
                    } else {
                        Toast.makeText(this, "请先开启浮窗权限", Toast.LENGTH_SHORT).show();
                    }
                }
        );

        if (checkOverlayPermission()) {
            createFloatingTextView();
        } else {
            requestOverlayPermission();
        }

        Button btnTest = findViewById(R.id.btn_test);

        btnTest.setOnClickListener(v -> {
            Log.i(TAG, "btnTest click");
        });
    }

    private boolean checkOverlayPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(this)) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    private void requestOverlayPermission() {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        overlayPermissionLauncher.launch(intent);
    }

    private void createFloatingTextView() {
        Intent intent = new Intent(this, FloatingTextViewService.class);
        startService(intent);
    }
}
5、Test
  • 页面中的按钮,因为悬浮常驻文本的遮挡,所以很难点击到

二、悬浮常驻文本遮挡处理策略

  1. 设置 FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCHABLE 标志,这是创建不可交互悬浮窗的关键,即窗口无法获得焦点,也无法接受触摸事件
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY :
                WindowManager.LayoutParams.TYPE_PHONE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, // 使触摸事件穿透
        PixelFormat.TRANSLUCENT
);
  1. 或者,单独设置 FLAG_NOT_FOCUSABLE 标志
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY :
                WindowManager.LayoutParams.TYPE_PHONE,
        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, // 使触摸事件穿透
        PixelFormat.TRANSLUCENT
);
标志 说明
FLAG_NOT_FOCUSABLE 窗口能否获得焦点,设置后,窗口无法获取键盘焦点
FLAG_NOT_TOUCHABLE 窗口是否接受触摸事件,设置后,触摸事件可以穿透传递给后面的窗口

三、FLAG_NOT_FOCUSABLE 的效果体现

1、Service
  • FloatingEditTextService.java
public class FloatingEditTextService extends Service {

    private LinearLayout llFloating;
    private WindowManager windowManager;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

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

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        // 创建布局参数
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ?
                        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY :
                        WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );

        // 设置位置为左下角
        params.gravity = Gravity.BOTTOM | Gravity.START;
        params.x = 0;
        params.y = 0;

        LayoutInflater inflater = LayoutInflater.from(this);
        llFloating = (LinearLayout) inflater.inflate(R.layout.floating_edit_text, null);

        // 添加到窗口
        windowManager.addView(llFloating, params);
    }

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

        if (llFloating != null) {
            windowManager.removeView(llFloating);
        }
    }
}
  • floating_edit_text.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#22FF0000"
    android:orientation="vertical"
    android:padding="20dp">

    <EditText
        android:id="@+id/et_test"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />
</LinearLayout>
2、Manifest
  • AndroidManifest.xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<application>

    ...

    <service
        android:name=".service.FloatingEditTextService"
        android:enabled="true"
        android:exported="false" />
</application>
3、Activity Layout
  • activity_floating_edit_text_service.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FloatingEditTextServiceActivity">

</androidx.constraintlayout.widget.ConstraintLayout>
4、Activity Code
  • FloatingEditTextServiceActivity.java
public class FloatingEditTextServiceActivity extends AppCompatActivity {

    private ActivityResultLauncher<Intent> overlayPermissionLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_floating_edit_text_service);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        test();
    }

    private void test() {
        overlayPermissionLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (checkOverlayPermission()) {
                        createFloatingTextView();
                    } else {
                        Toast.makeText(this, "请先开启浮窗权限", Toast.LENGTH_SHORT).show();
                    }
                }
        );

        if (checkOverlayPermission()) {
            createFloatingTextView();
        } else {
            requestOverlayPermission();
        }
    }

    private boolean checkOverlayPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (Settings.canDrawOverlays(this)) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    private void requestOverlayPermission() {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        overlayPermissionLauncher.launch(intent);
    }

    private void createFloatingTextView() {
        Intent intent = new Intent(this, FloatingEditTextService.class);
        startService(intent);
    }
}
5、Test
  1. 设置 FLAG_NOT_FOCUSABLE 标志,无法使用 EditText 输入内容

  2. 设置参数为 0,即可使用 EditText 输入内容

  • 注:设置 FLAG_NOT_FOCUSABLEFLAG_NOT_TOUCHABLEFLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCHABLE 标志,都无法使用 EditText 输入内容
Logo

智能硬件社区聚焦AI智能硬件技术生态,汇聚嵌入式AI、物联网硬件开发者,打造交流分享平台,同步全国赛事资讯、开展 OPC 核心人才招募,助力技术落地与开发者成长。

更多推荐