Android:更改背景颜色查看onClick按钮

时间:2014-03-27 09:07:37

标签: android view colors background

点击View时,如何在按钮下将背景颜色更改为button?我试过selector不起作用,因为View不会改变颜色。问题是什么?

这是我的代码:

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));
    }
}

但代码不起作用

7 个答案:

答案 0 :(得分:0)

你错误地使用drawable使用这一个

 view.setBackgroundResource(R.drawable.linea_verde)

答案 1 :(得分: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));

          }
    });

答案 2 :(得分:0)

setBackgroundColor()仅用于颜色,但似乎您使用状态列表可绘制。如果您确定要根据Button的状态使用其他颜色,请使用以下代码设置状态列表drawable:

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>

答案 3 :(得分:0)

你也可以这样做。

android:background="@drawable/linea_verde"

答案 4 :(得分:0)

如果您想应用所选项目的默认背景,可以将data = [ { 'name': 'john', 'age': 10 }, { 'name': 'paul', 'age': 20 } ] age = -1 # in case the name is not found for d in data: if d['name'] == 'john': age = d['age'] print(age) 属性与android background一起使用。

?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" /> 属性将根据Android版本使用正确的drawable。在棒棒糖前,它将使用坚实的背景,但在棒棒糖设备上将使用涟漪效应。

代码来自: com.android.support/appcompat-v7/23.0.0/res/values/values.xml

selectableItemBackground

答案 5 :(得分:0)

public void onClick(View v) {
    int id = v.getId();
    if(id == R.id.button) {
        linea2.setBackgroundResource(R.drawable.linea_verde);
    }
}

我认为你是以错误的方式比较观点。我碰巧偶然发现了这个问题,因为它没有被标记为已回答,这可能对其他人有所帮助。

答案 6 :(得分:0)

为您的形状创建两个单独的文件,并在按钮单击后更改视图的背景:

private boolean isPressed = false;    

public void onClick(View v) {
    if(v == botonMetro) {
        if(isPressed){
           linea2.setBackgroundResource(R.drawable.pressed_shape);       
        } else{
           linea2.setBackgroundResource(R.drawable.un_pressed_shape);       
        }
        isPressed = !isPressed;
    }
}
相关问题