2014-03-27 84 views
0

當我點擊button時,如何將背景顏色更改爲View?我試過selector不起作用,因爲視圖沒有改變顏色。問題是什麼?Android:更改背景顏色查看onClick按鈕

這是我的代碼:

XML

... 
      <View 
      android:id="@+id/viewPlanos2" 
      android:layout_width="match_parent" 
      android:layout_height="3dp" 
      android:layout_gravity="center" /> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:background="@color/transparente" 
      android:drawableTop="@drawable/buttonimage" 
      android:gravity="center" 
      android:paddingTop="50dp" /> 

     <View 
      android:id="@+id/viewPlanos1" 
      android:layout_width="match_parent" 
      android:layout_height="3dp" 
      android:layout_gravity="center" /> 
... 

JAVA

View linea2 = (View)findViewById(R.id.viewPlanos2); 
linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde)); 

linea_verde

<item android:state_pressed="true"><shape> 
     <gradient android:angle="90" android:endColor="@color/azul" android:startColor="@color/azulOscuro" /> 
    </shape></item> 
<item android:state_focused="true"><shape> 
     <gradient android:angle="90" android:endColor="@color/azul" android:startColor="@color/azulOscuro" /> 
    </shape></item> 
<item><shape> 
     <solid android:color="@color/rojo" /> 
    </shape></item> 

編輯:

我曾嘗試:

public void onClick(View v) { 

    if(v == botonMetro) { 

     linea2.setBackgroundResource(R.drawable.linea_verde); 
        and 

     linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde)); 
    } 
} 

回答

0

您錯誤地使用可繪製使用這一項上的代碼無法正常工作Ë

view.setBackgroundResource(R.drawable.linea_verde) 
+0

我已編輯我的代碼 – Charlie

0

就像你說的改變背景顏色爲查看該按鈕下,當我按一下按鈕

做這樣

button.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 

        // change color of your view here 
      linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde)); 

      } 
    }); 
+0

我已編輯我的代碼 – Charlie

+0

仍然無法工作? – MilapTank

+0

不,不起作用 – Charlie

0

setBackgroundColor()僅適用於顏色,但它似乎您使用可繪製的狀態列表。如果你確定要使用不同的顏色取決於Button的狀態下,使用此代碼設置狀態列表繪製:

view.setBackgroundDrawable(R.drawable.linea_verde); 

否則,只需設置使用

view.setBackgroundColor(getResources().getColor(R.drawable.yourcolor); 

背景顏色但在這種情況下,請使用點擊監聽器,並確保實際使用顏色資源,例如:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="opaque_red">#f00</color> 
    <color name="translucent_red">#80ff0000</color> 
</resources> 
+0

我編輯了我的代碼 – Charlie

+0

您必須指定它如何不工作。 –

+0

直接在'onCreate()'中使用setBackgroundDrawable。 –

0

您也可以這樣做。

android:background="@drawable/linea_verde" 
0

如果你想申請所選項目的默認背景,你可以使用background屬性與Android ?attr

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="?attr/selectableItemBackground" /> 

selectableItemBackground屬性將根據Android版本使用適當的drawable。在預棒棒糖上它會使用堅實的背景,但在棒棒糖設備上會使用漣漪效應。

代碼來自:com.android.support/appcompat-v7/23.0.0/res/values/values.xml

<item name="selectableItemBackground">@drawable/abc_item_background_holo_dark</item> 
0
public void onClick(View v) { 
    int id = v.getId(); 
    if(id == R.id.button) { 
     linea2.setBackgroundResource(R.drawable.linea_verde); 
    } 
} 

我覺得你比較視圖中的錯誤的方式。我碰巧碰到這個問題,既然它沒有被標記爲回答,這可能有助於其他人。