2013-12-23 85 views
0

我有以下代碼:側面相對佈局中的線性佈局我想更改按下時相對佈局的顏色。

選擇代碼:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true"><shape> 
      <solid android:color="@color/red" /> 
     </shape></item> 


</selector> 

佈局代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/itemLayoutView" 
    android:layout_width="match_parent" 
    android:layout_height="70dp" 
    android:clickable="true" 
    android:background="@drawable/listview_row_background_selector" 
    android:paddingLeft="@dimen/list_view_layout_margin" > 

    <LinearLayout 
     android:id="@+id/itemLinearLayoutView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="5dp" 
     android:layout_marginTop="5dp" 
     android:clickable="true" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/gameNameView" 
      android:layout_width="wrap_content" 
      android:layout_height="36dp" 
      android:textSize="@dimen/game_list_text_size" /> 

     <TextView 
      android:id="@+id/gameCreationDateView" 
      android:layout_width="wrap_content" 
      android:layout_height="24dp" 
      android:textColor="@color/gray" 
      android:textSize="@dimen/small_text_size" 
      android:textStyle="italic" /> 
    </LinearLayout> 

</RelativeLayout> 

我想改變相對佈局的顏色,當我點擊它。但是,上面的代碼不起作用。

有什麼建議嗎?

注:我在我的列表視圖中使用佈局作爲代碼的一部分。

回答

2

如果你正在使用你的佈局作爲listview的一部分,那麼你爲什麼給選擇器那佈局。你可以在listview中使用選擇器。

請試試看。它有時可能會幫助您:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="70dp" 
    > 

<RelativeLayout 
    android:id="@+id/itemLayoutView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true" 
    android:background="@drawable/listview_row_background_selector" 
    android:paddingLeft="@dimen/list_view_layout_margin" > 

    <LinearLayout 
     android:id="@+id/itemLinearLayoutView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="5dp" 
     android:layout_marginTop="5dp" 
     android:clickable="true" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/gameNameView" 
      android:layout_width="wrap_content" 
      android:layout_height="36dp" 
      android:textSize="@dimen/game_list_text_size" /> 

     <TextView 
      android:id="@+id/gameCreationDateView" 
      android:layout_width="wrap_content" 
      android:layout_height="24dp" 
      android:textColor="@color/gray" 
      android:textSize="@dimen/small_text_size" 
      android:textStyle="italic" /> 
    </LinearLayout> 


</RelativeLayout> 
</RelativeLayout> 
+0

我不能我有我自己的自定義偵聽器的列表查看所以列表視圖選擇不起作用。 –

+0

真的是你喜歡做什麼。你能簡單解釋我嗎? –

+0

好吧,我將我自己的自定義偵聽器添加到相對視圖的列表視圖行中,以實現向左滑動和向右滑動等功能。我注意到,點擊ListView後添加監聽器不會再改變listview項目的背景。所以我想我必須將它添加到RelativeLayout,它是列表視圖中包含的元素。 –

0

可能,這將有助於..

list_selector.xml:

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

</selector> 

list_selector_pressed.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <shape android:shape="rectangle" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 
     <solid android:color="#5D9438"/> 
     <size android:width="1dip"/> 
     <size android:height="1dip"/> 

    </shape> 

添加android:background="@drawable/list_selector"到UR相對佈局..

+0

我測試了我自己的選擇,選擇作品上的LinearLayout的RelativeLayout的孩子,如果我把機器人:後臺=「@繪製/ listview_row_background_selector」:點擊=「真」 機器人線性佈局。所以,我認爲它與選擇器代碼沒有任何關係。 –

+0

我想這個問題是檢測當我按相對佈局,因爲線性佈局是在相對佈局的頂部。 –

+0

因爲你保持匹配父線性佈局,它消耗整個相對佈局..可能是沒有觸摸REL佈局是可能的..它只適用於線性佈局 – Jarvis