Android ImageButton更改背景

时间:2014-06-05 13:53:46

标签: android background imagebutton

有没有办法在不为该状态创建另一个按钮的情况下更改ImageButton的背景?

我的意思是,当你添加一个ImageButton时,它会获得一个灰色背景,并在选中时会变成稍暗的灰色。

添加:android:background="@color/white",会使我的背景变白(就像我想要的那样),但选择按钮时会发生注意。这样的事情会很好: android:background_selected ="the color you want here"

我想要一个背景处于正常状态且按下该按钮时背景为蓝色背景的按钮。

6 个答案:

答案 0 :(得分:4)

您需要在可绘制文件夹中创建一个xml文件,其内容如下:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/normal_background" android:state_pressed="false"/>
    <item android:drawable="@drawable/pressed_background" android:state_pressed="true"/>
</selector>

将此作为背景附加到您的按钮:android:background =&#34; @ drawable / filename&#34;

来源:http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

答案 1 :(得分:0)

我的第一条建议是使用普通按钮,然后将其更改为白色,您可以通过Android Studio的XML编辑器轻松完成,如果您正在使用它,然后打开它单击时为蓝色,因为这是从Android Studio创建的应用程序的默认属性。

答案 2 :(得分:0)

OnTouchListener使用ImageButton

int SELECTED_COLOR = Color.RED; //set to your color
int UNSELECTED_COLOR = Color.BLUE; //set to your color

OnTouchListener onTouchListener = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
           switch(event.getAction())
           {
               case MotionEvent.ACTION_DOWN :
                   v.setBackgroundColor(SELECTED_COLOR); //selected color
                   break;
               case MotionEvent.ACTION_UP :
                   v.setBackgroundColor(UNSELECTED_COLOR); //unselected color
                     break;
           }
           return true;
    }
};

imageButton.setBackgroundColor(UNSELECTED_COLOR);
imageButton.setOnTouchListener(onTouchListener);

答案 3 :(得分:0)

在drawable文件夹中创建两个xml形状:

shape_1.xml:

      <?xml version="1.0" encoding="UTF-8" ?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
      <solid android:color="@android:color/white" />

      </shape>

shape_2.xml

     <?xml version="1.0" encoding="UTF-8" ?>
     <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
     <solid android:color="@android:color/black" />

     </shape>

在drawable文件夹中创建一个selector_1.xml:

     <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:drawable="@drawable/shape_1"
      android:state_pressed="true" />
     <item android:drawable="@drawable/shape_2"
      android:state_pressed="false" />
     <item android:drawable="@drawable/shape_2" />
    </selector>

并将选择器设置为按钮的src:

            <ImageButton
    android:id="@+id/imageButton_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/selector_1" />

答案 4 :(得分:0)

非常感谢大家的时间。我在这里找到了一个很好的解决方案: Standard Android Button with a different color

只需要您创建一个额外的xml文件。

简化如下:

  1. 在res。中创建一个可绘制的文件夹。
  2. 在该文件夹中创建custom_button.xml(选择选择器作为根元素)。
  3. 通过此代码并链接您的布局按钮,如下所示:android:background =&#34; @ drawables / custom_button&#34;

    <item android:state_pressed="true" >
        <shape>
            <gradient
                android:startColor="@color/yellow1"
                android:endColor="@color/yellow"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="@color/black" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="20dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    
    <item>        
        <shape>
            <gradient
                android:endColor="@color/white"
                android:startColor="@color/white"
                android:angle="270" />
            <padding
                android:left="10dp"
                android:top="20dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    

答案 5 :(得分:-1)

在drawable文件夹中创建一个名为imagebutton_selector的xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" android:color="@color/blue" />
<item android:color="@color/white" />

</selector>

,而不是你在布局xml中添加ImageButton设置背景,如下所示:

<ImageButton
    ...
    android:background="@drawable/imagebutton_selector"
    ...
 />