2012-10-24 36 views
0

我習慣於沒有任何問題(即使有複雜的佈局)的listview項目處理onpress事件(使它按下和未按下的某些操作)。但是現在我想要從LinearLayout獲得同樣的效果 - 如果您仍然按佈局,所有後代都應該使用onpress事件。如何讓子視圖從根視圖中獲得「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> 

onpress的根佈局變化的背景,但後代textcolor不。看起來他們沒有從根本上得到事件。

那麼,你能幫忙嗎?

UPDATE:LinearLayout中ll_about_fragment_feedback已經結合OnClickListener並有一切罰款selecrors

+0

@Asok它設置五言 – jnthnjns

+0

。我的意思是讓它受到壓制的東西。不要點擊 – bluebyte

回答

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> 
0

只需綁定你onClickListener根佈局(ll_about_fragment_feedback),就是這樣的對象。

+0

我知道,有沒有onPress事件爲什麼不設置一個'onClickListener'到容器中,'LinearLayout'你的情況 – bluebyte

+1

@bluebyte你的意思是你在尋找一個'Selector'嗎?類似於[this](http://stackoverflow.com/a/5374283/1134705) – jnthnjns

+0

如果您詢問關於點擊的視覺反饋,那麼聽衆與它沒有任何共同之處。正如Asok所說,你需要使用正確的選擇器。 –

相關問題