【问题标题】:How to overlay a button programmatically?如何以编程方式覆盖按钮?
【发布时间】:2018-08-27 14:23:32
【问题描述】:

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

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

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

【问题讨论】:

    标签: android view dialog overlay


    【解决方案1】:

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

    【讨论】:

    • 效果很好。谢谢。您知道无需更改活动 xml 布局即可完成此操作的方法吗?我的应用程序中的所有活动都需要这个,并且必须将所有活动的布局包装到一个框架布局中以便在需要时让这个弹出按钮出现在它们上面似乎效率低下。
    • 您可以创建一个 Activity 的子类,所有的 Activity 都扩展它,并在那里有一些逻辑以编程方式将特定布局插入到 FrameLayout 中。
    【解决方案2】:

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

    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 实例(私有处理程序 messageHandler)的成员。我使用的 Android 4.1 设备低于我不知道会发生什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-18
      • 2010-12-07
      • 1970-01-01
      • 2020-06-19
      • 2018-02-25
      • 2021-05-10
      • 2014-12-10
      • 1970-01-01
      相关资源
      最近更新 更多