2015-10-07 63 views
1

請參閱下面的佈局。如果用戶觸摸ImageView或優選整個佈局,我想在整個LinearLayout上顯示選擇高亮顯示。這就像一個onclick效果。雖然我有一個選擇器不工作。我該如何做這項工作?在LinearLayout上單擊顯示選擇遮罩效果

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:background="@drawable/layout_selector" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/btnFromEasy" 
     android:layout_width="@dimen/normal_button_size" 
     android:layout_height="@dimen/normal_button_size" 
     android:adjustViewBounds="true" 
     android:scaleType="fitCenter" 
     android:src="@drawable/animals" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:text="@string/easy" 
     android:textColor="@color/subtitle_text_color" 
     android:textSize="@dimen/text_size_small" 
     android:textStyle="bold" /> 

</LinearLayout> 


    <?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true"> 
     <color android:color="Blue" /> 
    </item> 
    <item> 
     <color android:color="White" /> 
    </item> 
</selector> 
+0

請參閱http://stackoverflow.com/問題/ 10876119/android-how-can-i-create-a-selector-for-layouts-like-an-imagebutton-selector – headuck

回答

2

試試這個簡單的代碼。

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
     android:drawable="@drawable/background_image_pressed" /> // image  
    <item 
     android:drawable="@drawable/background_image_default" /> // image 
</selector> 

和裏面的LinearLayout添加android:clickable="true"

<LinearLayout 
    ... 
    android:clickable="true" 
    ...> 

希望這有助於。

+0

感謝您的回覆。 – pats

1

添加機器人:點擊= 「true」 以LinearLayout中的屬性和它的工作

您的代碼:

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:background="@drawable/layout_selector" 
    android:orientation="vertical"> 

- >添加

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:background="@drawable/layout_selector" 
     android:orientation="vertical" 

     android:clickable="true"> 

android:layout_width="0dp"線使得佈局可以」 t是可見的,所以我將其更改爲"wrap_content"

+0

它是否適用於線性佈局? –

+0

這裏是我的代碼:https://github.com/thantieuhodo/LinearLayoutClickEffect – thantieuhodo

+0

感謝您的回覆。我厭倦了這一點,突出顯示只有當我點擊空格控件不會與線性佈局重疊時纔會發生。我也試過android:descendantFocusability =「blocksDescendants」,但是沒有做出改變。請注意,線性佈局也是其他兩種線性佈局的子項。 – pats