如何以编程方式覆盖按钮?

发布于 2024-09-28 13:10:55 字数 204 浏览 5 评论 0原文

我想要完成的是,在运行时,在屏幕中间放置一个按钮,作为最顶层,覆盖其下面的任何内容。 (它不大,所以它不会完全覆盖屏幕,只是它下面发生的任何事情。)

我考虑创建一个自定义对话框,但是它会阻止所有其他用户输入。我希望这个新按钮下方的所有视图都能正常运行并响应用户,但我只想在所有内容之上添加(然后删除)该按钮。

希望这是有道理的。我只是想知道最好的研究方法是什么?

What I would like to accomplish is to, at runtime, place a button in the middle of the screen, as the very top layer, overlaying anything below it. (It's not big, so it will not completely cover the screen, just whatever happens to be below it.)

I looked at creating a custom dialog, however that blocks all other user input. I want all of the views below this new button to act normally and respond to the user, but I just want to add (and later remove) the button above everything.

Hopefully that makes sense. I'm just wondering what might be the best approach to look into?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

找回味觉 2024-10-05 13:10:55

使用 FrameLayout,并将按钮作为第二个子元素。当您不希望它可见时,将其设置为“消失”。

Use a FrameLayout, with the button as it's 2nd child. Set it to GONE when you don't want it visible.

情魔剑神 2024-10-05 13:10:55

我必须以编程方式在任何可见活动之上覆盖一个简单的布局。正常的活动布局 xml 对覆盖层一无所知。布局只有一个文本视图组件,但可以具有您认为合适的任何结构。这是我的叠加布局。

res/layout/identity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/identitylayout"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_centerInParent="true" >

<TextView 
    android:id="@+id/identityview"
    android:padding="5dp"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:textColor="#FFFFFF" android:background="#FF6600"
    android:textSize="30dp"            
/>

</RelativeLayout>

在从屏幕上删除超时后,覆盖显示在现有内容之上。应用程序调用此函数来显示叠加。

private void showIdentity(String tag, long duration) {
    // default text with ${xx} placeholder variables
    String desc = getString(R.string.identity);
    desc = desc.replace("${id}", reqId!=null ? reqId : "RequestId not found" );
    desc = desc.replace("${tag}", tag!=null ? tag : "" );
    desc = desc.trim();

    // get parent and overlay layouts, use inflator to parse
    // layout.xml to view component. Reuse existing instance if one is found.
    ViewGroup parent = (ViewGroup)findViewById(R.id.mainlayout);
    View identity = findViewById(R.id.identitylayout);
    if (identity==null) {
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        identity = inflater.inflate(R.layout.identity, parent, false);
        parent.addView(identity);
    }

    TextView text = (TextView)identity.findViewById(R.id.identityview);
    text.setText(desc);
    identity.bringToFront();

    // use timer to hide after timeout, make sure there's only
    // one instance in a message queue.
    Runnable identityTask = new Runnable(){
        @Override public void run() {
            View identity = findViewById(R.id.identitylayout);
            if (identity!=null)
                ((ViewGroup)identity.getParent()).removeView(identity);
        }
    };
    messageHandler.removeCallbacksAndMessages("identitytask");
    messageHandler.postAtTime(identityTask, "identitytask", SystemClock.uptimeMillis()+duration);
}

Timer messageHandler 是主 Activity 实例(私有 Handler messageHandler)的成员,我将所有计划任务放置在其中。我使用的 Android 4.1 设备低于该版本,我不知道会发生什么。

I had to overlay a simple layout programmatically on top of any visible activity. Normal activity layout xmls don't know anything about the overlay. Layout had one textview component but could have any structure you see fit. This is my overlay layout.

res/layout/identity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/identitylayout"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_centerInParent="true" >

<TextView 
    android:id="@+id/identityview"
    android:padding="5dp"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:textColor="#FFFFFF" android:background="#FF6600"
    android:textSize="30dp"            
/>

</RelativeLayout>

Overlay is shown on top of the existing content, after timeout is deleted from the screen. Application calls this function to display overlay.

private void showIdentity(String tag, long duration) {
    // default text with ${xx} placeholder variables
    String desc = getString(R.string.identity);
    desc = desc.replace("${id}", reqId!=null ? reqId : "RequestId not found" );
    desc = desc.replace("${tag}", tag!=null ? tag : "" );
    desc = desc.trim();

    // get parent and overlay layouts, use inflator to parse
    // layout.xml to view component. Reuse existing instance if one is found.
    ViewGroup parent = (ViewGroup)findViewById(R.id.mainlayout);
    View identity = findViewById(R.id.identitylayout);
    if (identity==null) {
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        identity = inflater.inflate(R.layout.identity, parent, false);
        parent.addView(identity);
    }

    TextView text = (TextView)identity.findViewById(R.id.identityview);
    text.setText(desc);
    identity.bringToFront();

    // use timer to hide after timeout, make sure there's only
    // one instance in a message queue.
    Runnable identityTask = new Runnable(){
        @Override public void run() {
            View identity = findViewById(R.id.identitylayout);
            if (identity!=null)
                ((ViewGroup)identity.getParent()).removeView(identity);
        }
    };
    messageHandler.removeCallbacksAndMessages("identitytask");
    messageHandler.postAtTime(identityTask, "identitytask", SystemClock.uptimeMillis()+duration);
}

Timer messageHandler is member of main Activity instance (private Handler messageHandler) where I put all scheduled tasks. I am using Android 4.1 device lower than that I don't know what happens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文