You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin开发:让Android的ActionProcessText Activity显示在其他应用上层

问题:让绑定ACTION_PROCESS_TEXT的Activity悬浮在其他应用上层

我正在开发一个Android应用,需要创建一个绑定系统ACTION_PROCESS_TEXT意图的Activity,目标是让这个Activity的布局直接显示在当前运行的应用界面上方。目前Activity能正常启动,但只会单独打开新页面,无法悬浮在其他应用上层。

谷歌翻译已经实现了相同的效果,并且我确认它不需要申请「在其他应用上层显示」权限——它的布局会直接在浏览器等应用的界面上方弹出,而非切换到新页面。

以下是我用Xamarin编写的Activity代码:

[Activity(Label = "@string/app_name", Exported = true)]
[IntentFilter(new[] { Intent.ActionProcessText, Intent.CategoryDefault }, DataMimeType = "text/plain")]
public class LookupActivity : Activity
{
    public override void OnCreate(Bundle? savedInstanceState, PersistableBundle? persistentState)
    {
        base.OnCreate(savedInstanceState, persistentState);
        var data = this.Intent.GetCharSequenceExtra(Android.Content.Intent.ExtraProcessText);
    }
}

对应的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LookupActivity"
    android:alpha="0.2">
 
    <TextView
        android:id="@+id/msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="16dp"
        android:text="Button"
        android:onClick="OnButtonClicked"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/msg"
        app:layout_constraintVertical_bias="0.025" />
 
</android.support.constraint.ConstraintLayout>

我原本以为将布局的Alpha值设置为0.2,就能模仿谷歌翻译那种带阴影背景的悬浮效果,但实际测试时,从系统上下文菜单选择我的应用后,Activity是作为独立页面启动的,完全没有悬浮在原应用上方的效果。

内容的提问来源于stack exchange,提问作者Loligans

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 02:17:35