【问题标题】:Android: change background color View onClick ButtonAndroid:更改背景颜色查看 onClick 按钮
【发布时间】:2014-05-06 03:22:00
【问题描述】:

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

这是我的代码:

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

但代码不起作用

【问题讨论】:

    标签: android view colors background


    【解决方案1】:

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

     view.setBackgroundResource(R.drawable.linea_verde)
    

    【讨论】:

      【解决方案2】:

      正如你所说,当我单击按钮时,将背景颜色更改为在按钮下查看此内容

      这样做

       button.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
      
                        // change color of your view here  
                 linea2.setBackgroundColor(getResources().getColor(R.drawable.linea_verde));
      
                }
          });
      

      【讨论】:

        【解决方案3】:

        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>
        

        【讨论】:

        • onCreate()中直接使用setBackgroundDrawable。
        【解决方案4】:

        你也可以这样做。

        android:background="@drawable/linea_verde"
        

        【讨论】:

          【解决方案5】:

          如果您想应用所选项目的默认背景,您可以使用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 版本使用适当的可绘制对象。在棒棒糖之前,它将使用纯色背景,但在棒棒糖设备上,将使用波纹效果。

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

          <item name="selectableItemBackground">@drawable/abc_item_background_holo_dark</item>
          

          【讨论】:

            【解决方案6】:
            public void onClick(View v) {
                int id = v.getId();
                if(id == R.id.button) {
                    linea2.setBackgroundResource(R.drawable.linea_verde);
                }
            }
            

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

            【讨论】:

              【解决方案7】:

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

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2021-07-05
                • 1970-01-01
                • 2015-06-04
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多