其他分享
首页 > 其他分享> > android – 如何创建一个浮动的可触摸活动仍然允许触摸其边界外的本机控件?

android – 如何创建一个浮动的可触摸活动仍然允许触摸其边界外的本机控件?

作者:互联网

我试图实现的目标最好用我用mspaint制作的方案来解释:

enter image description here

我试图设置FLAG_NOT_TOUCH_MODAL,其描述应该是我想要的,但它根本不起作用.我的活动消耗所有触摸事件,甚至在其边界之外.

如果我设置FLAG_NOT_FOCUSABLE,那么当然活动下的本机控件是可触摸的,但是当触摸其边界内时,活动完全不均匀.

我已经尝试在清单中设置isFloatingWindow = true,但它似乎没有任何区别.

谁能实现这个目标?我非常感谢以这种方式工作的小型演示活动,因此我可以从那里开始工作.我已经尝试了很多WindowManager和Intent标志的排列,似乎没有任何东西可以完全按照我的需要工作.

提前致谢.

更新:

我已经尝试了你的建议,但它仍然无法正常工作.

这是我的活动布局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);
}

不幸的是,这是结果:

Does not look like a dialog

我错过了什么?

谢谢.

解决方法:

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

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

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

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);     
}

标签:floating,android,android-activity
来源: https://codeday.me/bug/20191005/1856289.html