在安卓中是否可以像窗口一样使视图不可触摸?

3

可以通过以下代码将整个窗口设置为不可触摸,这意味着您可以单击该活动后面的任何内容:

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

现在我的问题是:是否可能使RelativeLayout不可触摸,我的意思是用户可以触摸RelativeLayout后面的任何东西,但他将无法触摸RelativeLayout中按钮后面的任何东西。
以下是我的xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    style="@android:color/transparent"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="44dp"
        android:text="Button" />


</RelativeLayout>

我希望可以使相对布局(RelativeLayout)不可点击,让用户可以在其后面的内容上进行操作。

我只需要相对布局中的按钮可以被点击。

因此,按钮可以被点击,相对布局不可被点击。我可以点击按钮,但是我不能点击相对布局,只能点击它后面的内容。

使用以下代码:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

会使整个窗口都变成不可点击,包括按钮,这不是我想要的。

我希望我的问题很清晰明了。

编辑:

我只想在另一个应用程序(例如Skype)上显示一个按钮。我只希望在Skype应用程序中可以点击该按钮。


这正是我正在寻找的 :) 你有找到任何解决方案吗?如果有,请发布它。 - Shirish Herwade
2个回答

2

1
在你的Java代码中写成这样。
ArrayList<View> touchables;
RelativeLayout RelativeLayout_ = (RelativeLayout) findViewById(R.id. rl);
touchables = RelativeLayout_.getTouchables();
for (View touchable : touchables) 
    {
        if (!(touchable.getId()==R.id.button1))
        {
            touchable.setEnabled(false);
        }
    }

希望它能够正常工作。

2
这个没能很好地运作。我无法通过rl视图传递触摸事件。 - ARMAGEDDON
我没听懂你的意思!你在那个XML文件中只使用了这个相对布局还是用了其他布局? - Amsheer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接