如何创建一个浮动的可触摸活动,该活动仍然允许触摸其边界之外的本机控件? [英] How to create a floating touchable activity that still allows to touch native controls outside of its borders?

查看:97
本文介绍了如何创建一个浮动的可触摸活动,该活动仍然允许触摸其边界之外的本机控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用mspaint制定的方案可以最好地解释我要实现的目标:

What I am trying to achieve is best explained by the scheme I made with mspaint:

我试图设置FLAG_NOT_TOUCH_MODAL,根据说明它应该正是我想要的,但是它根本不起作用.我的活动消耗了 ALL 触摸事件,甚至超出了边界.

I have tried to set FLAG_NOT_TOUCH_MODAL which by the description is supposed to be exactly what I want, but it simply does not work. My activity consumes ALL touch events, even outside its borders.

如果我设置FLAG_NOT_FOCUSABLE,那么该活动下的本机控件当然是可触摸的,但是即使在其内部触摸时,该活动也完全无法触摸.

If I set FLAG_NOT_FOCUSABLE then of course the native controls under the activity are touchable, but then the activity is completely not even when touching inside its borders.

我尝试在清单中设置isFloatingWindow=true,但似乎没有任何作用.

I have tried setting isFloatingWindow=true in the manifest, but it didn't seem to make any difference.

有人能做到吗?我将非常感谢以这种方式进行的小型演示活动,因此我可以从那里开始并开展工作.我已经为WindowManager和Intent标志尝试了许多排列,但似乎没有什么能像我需要的那样工作.

Can anyone achieve this? I would really appreciate a small demo activity that works this way so I can take it and work from there. I have tried numerous permutations for WindowManager and Intent flags and nothing seems to work exactly as I need.

谢谢.

更新:

我已经尝试过您的建议,但是仍然无法按需工作.

I have tried your suggestion, but it still did not work as desired.

这是我的活动布局xml:

This is my activity layout xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="385dp"
android:layout_height="300dp"
android:theme="@android:style/Theme.Dialog"
tools:context="com.ui.activities.TestActivity"
android:id="@+id/testLayout"
android:visibility="visible"
android:background="@drawable/abc_ab_solid_light_holo"
android:clickable="true">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="35dp"
    android:clickable="true"
    android:enabled="true"
    android:onClick="onClick" />

这是Activity类:

public class TestActivity extends Activity implements View.OnClickListener {

private String TAG = TestActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    setWindowParams();
}

private void setWindowParams() {
    WindowManager.LayoutParams wlp = getWindow().getAttributes();
    wlp.dimAmount = 0;
    wlp.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
    getWindow().setAttributes(wlp);
}

不幸的是,这是结果:

我想念什么?

谢谢.

推荐答案

在清单中的Activity上设置Dialog主题.例如:

Set a Dialog theme on the Activity in your manifest. For example:

android:theme="@android:style/Theme.Dialog"

然后在onCreate()中设置以下Window参数:

Then set the following Window parameters in onCreate():

public void setWindowParams() {
    WindowManager.LayoutParams wlp = getWindow().getAttributes();
    wlp.dimAmount = 0;            
    wlp.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
    getWindow().setAttributes(wlp);     
}

这篇关于如何创建一个浮动的可触摸活动,该活动仍然允许触摸其边界之外的本机控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆