Android 与机器人的多次使用作斗争
我有一个带有根元素Android 与机器人的多次使用作斗争,android,android-layout,user-interface,touch,android-view,Android,Android Layout,User Interface,Touch,Android View,我有一个带有根元素LinearLayout的布局,其中包含另外两个LinearLayout,它们组成了两行。它们分别包含一个开关 实际上,它看起来有点像这样: 这是我的示例布局文件的外观: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout
LinearLayout
的布局,其中包含另外两个LinearLayout
,它们组成了两行。它们分别包含一个开关
实际上,它看起来有点像这样:
这是我的示例布局文件的外观:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout android:id="@+id/layout_row_1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Switch android:id="@+id/switch_row_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="@+id/layout_row_2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Switch android:id="@+id/switch_row_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
在我的onCreate()
中,我为两行调用它:
mLayoutRow1 = findViewById(R.id.layout_row_1);
mLayoutRow2 = findViewById(R.id.layout_row_2);
mSwitchRow1 = (Switch) findViewById(R.id.switch_row_1);
mSwitchRow2 = (Switch) findViewById(R.id.switch_row_2);
expandTouchArea(mSwitchRow1, mLayoutRow1);
expandTouchArea(mSwitchRow2, mLayoutRow2);
我无法理解的是,对于第1行,它可以工作,但是对于第2行(以及对expandTouchArea
的任何其他后续调用),它不工作
我还尝试将OnGlobalLayoutListener
发布到开关本身的事件消息队列中,而不是添加OnGlobalLayoutListener
,从而产生完全相同的行为
因此,到目前为止我无法回答的问题是:对于不同的线性布局
是否可能有多个触摸代理
,它们分别将触摸事件路由到一个布局中的不同开关
?我发现,当我在每行周围添加另一个线性布局
时,两个触摸代理
都可以工作
现在,我的示例布局文件如下所示:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/layout_row_1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Switch android:id="@+id/switch_row_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/layout_row_2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Switch android:id="@+id/switch_row_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
也许还有另一个解决办法。这一次似乎有点乏味
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/layout_row_1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Switch android:id="@+id/switch_row_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/layout_row_2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Switch android:id="@+id/switch_row_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>