2014-03-27 31 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); 
    } 
} 

我觉得你比较视图中的错误的方式。我碰巧碰到这个问题,既然它没有被标记为回答,这可能有助于其他人。