如何使子视图从根视图获得“onpress”事件?

时间:2012-10-24 22:20:35

标签: android

我已经习惯了listview的项目没有任何问题(即使是复杂的布局)处理onpress事件(一些使其按下和未按下的动作)。但是现在我想从LinearLayout那里得到同样的东西 - 如果你无论如何按布局,所有后代都应该得到onpress事件。

这是我的片段布局的一部分,应该在onpress事件中突出显示。

<LinearLayout
            android:id="@+id/ll_about_fragment_feedback"
            android:gravity="center_vertical"
            android:layout_height="54dp"
            android:background="@drawable/about_item_bkg"
            android:layout_width="match_parent">

        <ImageView
                android:layout_height="wrap_content"
                android:background="@drawable/about_item_feedback_icon"
                android:layout_width="wrap_content"
                android:layout_marginLeft="11dp"/>

        <TextView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/about_fragment_feedback"
                android:layout_marginLeft="14dp"
                android:textSize="16dp"
                android:textColor="@drawable/about_item_textcolor"/>

</LinearLayout>

根布局的背景改变了印记,但后代的文字颜色没有改变。看起来他们没有从root获得事件。

那么,你能帮忙吗?

更新:LinearLayout ll_about_fragment_feedback绑定了OnClickListener,并且选择一切正常

2 个答案:

答案 0 :(得分:1)

如果您希望Selector应用于LinearLayout下的所有观看次数,则只需将android:duplicateParentState="true"添加到所有子视图中。

<LinearLayout
    android:id="@+id/ll_about_fragment_feedback"
    android:layout_width="match_parent"
    android:layout_height="54dp"
    android:background="@drawable/about_item_bkg"
    android:gravity="center_vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="11dp"
        android:background="@drawable/about_item_feedback_icon"
        android:duplicateParentState="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="14dp"
        android:duplicateParentState="true"
        android:text="@string/about_fragment_feedback"
        android:textColor="@drawable/about_item_textcolor"
        android:textSize="16dp" />

</LinearLayout>

答案 1 :(得分:0)

只需将onClickListener绑定到布局的根对象(ll_about_fragment_feedback)就可以了。