2013-07-09 32 views
1

我想改變按鈕的文本顏色和修改圖像,當我點擊其父線性佈局。 圖像正在改變,但textcolor不會在線性佈局點擊時發生變化。TextColor的按鈕不會改變onLight的父LinearLayout

這是我在XML碼 -

<LinearLayout 
     android:id="@+id/btn_layout" 
     android:layout_width="0.0dip" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1.0" 
     android:gravity="center" 
     android:clickable="true" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/done" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="@string/done_with_accounts_action" 
      android:textColor="@drawable/done_button_text_selector" 
      android:focusable="false" 
      android:textSize="@dimen/done_button_text_size" /> 

     <ImageView 
      android:id="@+id/done_btn_arrow" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:paddingLeft="4dp" 
      android:focusable="false" 
      android:src="@drawable/done_btn_arrow_bg_selector" /> 
    </LinearLayout> 

**selector xml for button -** 

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:color="@color/done_btn_pressed_color" /> 
    <item android:state_pressed="true" android:color="@color/done_btn_pressed_color" /> 
    <item android:color="@color/button_text_white_color" /> 
</selector> 

如果我的TextView更換按鈕,一切工作正常。

回答

0

我找到了解決方案 - 我必須設置可點擊爲false按鈕,現在它也可以識別它的父級onClick事件。

爲什麼上述代碼與textView一起工作,因爲它的onClick事件被默認設置爲禁用,因此它可以識別它的父級onClick事件。

1

在這行:

<item android:state_focused="true" android:color="@color/done_btn_pressed_color" /> 
    <item android:state_pressed="true" android:color="@color/done_btn_pressed_color" /> 

你把相同的顏色done_btn_pressed_color。 也許這就是問題所在?

+0

不,這不是問題。默認顏色是不同的。 – Sunita

+0

您可以嘗試從代碼中進行更改。 – dasdasd

+0

我試過了。我不能將它設置爲線性佈局,因爲按鈕[change textcolor]和imageview [change image]有兩個不同的xml選擇器,並且按鈕佈局上的更改不起作用。 – Sunita