如何在另一个已经可点击的视图下方创建一个可点击的视图?

时间:2012-10-24 20:54:31

标签: android android-layout android-appwidget

很难给出这个问题的描述性标题!

我有一个应用程序小部件,其中有一些可点击按钮(使用应用程序小部件需要的RemoteViews和PendingIntents),这些按钮很难以当前大小点击。为了解决这个问题而不必调整实际按钮的大小,我在这些按钮上放置了较大的,不可见的,可点击的FrameLayouts来增加可点击区域。这很有效,除了按钮还有连接到它们的选择器,以便在点击时图像的背景颜色发生变化。当FrameLayout覆盖按钮时,此选择器不再起作用,因为它现在实际上是正在点击的FrameLayout,而不是按钮。

我的问题是,是否有办法以某种方式使其“点击”FrameLayout,以便按钮的选择器仍然被触发,或任何其他巧妙的解决方法达到相同的效果?在FrameLayout本身上应用选择器会使背景变化太大。

下面是我正在做的一个简化示例。我使用的实际布局更复杂,因此任何需要更改布局的解决方案都可能比这里看起来更难。此外,FrameLayout放在RelativeLayout中的原因是我对展示位置有更多自由。

修改

FrameLayout放在另外两个布局之上,这就是为什么我不能简单地将它放在按钮下面。它需要覆盖更大的区域。我希望我不必重做整个布局来解决这个问题。

布局

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

            <ImageButton
                android:layout_width="@dimen/size_myButton_w"
                android:layout_height="@dimen/size_myButton_h"
                android:layout_alignParentRight="true"
                android:background="@drawable/selector" //NOT TRIGGERED! Covered by FrameLayout below. 
                android:src="@drawable/myButton" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                <!-- More stuff here! -->

            </RelativeLayout>

                 <!-- Even more stuff here! -->

    </LinearLayout>

    <FrameLayout
        android:layout_width="120dp"
        android:layout_height="35dp"
        android:layout_alignParentRight="true">
    </FrameLayout>

</RelativeLayout>   

SELECTOR

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/navy" />
    <item android:state_focused="true" android:drawable="@color/white" />
    <item android:drawable="@color/white" />
</selector>

1 个答案:

答案 0 :(得分:1)

也许您可以将FrameLayout 放在 按钮下方,而不是放在顶部。这样,如果他们点击了实际的按钮,你就会触发你的选择器,如果他们错过了按钮并仍然按下FrameLayout ......好吧,你的选择器不会被触发,但你的FrameLayout点击监听器仍会被调用。

相关问题