有可能使视图不像Android中的窗口那样可触摸吗?

时间:2013-05-31 13:54:20

标签: android android-layout

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

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

现在我的问题是:是否有可能只使RelativeLayout不可触摸,我的意思是用户可以触摸那个RelativeLayout后面的任何东西,但他将无法触及那个RelativeLayout中Button背后的任何东西。

这是我的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设置为NOT TOUCHABLE,用户可以触摸它背后的任何内容。 我只需要relativelayout中的按钮可以触摸

因此,Button可触摸,RelativeLayout Not Touchable。 我可以触摸按钮,但我无法触摸RelativeLayout,我只触摸它背后的东西。

使用时 getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); 整个窗口被设置为不能与Button一起触摸,这不是我想要的。

我希望我的问题是可以理解的。

编辑:

我要做的就是在另一个应用程序上显示一个按钮(例如Skype) 我只希望该按钮可以与Skype应用程序一起点击。

2 个答案:

答案 0 :(得分:2)

尝试将android:duplicateParentState="true"添加到RelativeLayout这将在视图树中传播事件。

如果这不起作用,请尝试重写此方法。 http://developer.android.com/reference/android/view/ViewGroup.html#onInterceptTouchEvent(android.view.MotionEvent)

答案 1 :(得分:0)

  

在你的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);
        }
    }
  

希望它能运作