【问题标题】:Android ImageButton change backgroundAndroid ImageButton 改变背景
【发布时间】:2014-06-05 13:53:46
【问题描述】:

有没有办法改变 ImageButton 的背景而不为该状态创建另一个按钮?

我的意思是,当您添加 ImageButton 时,它会获得灰色背景,并且在选择时会变得更深一些。

添加:android:background="@color/white",将使我的背景变为白色(如我所愿),但在选择按钮时会发生注意事项。像这样的东西会很好: android:background_selected ="the color you want here"

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

【问题讨论】:

    标签: android background imagebutton


    【解决方案1】:

    你需要在drawable文件夹中创建一个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="@drawable/filename"

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

    【讨论】:

    • 但是使用这个我必须创建两个按钮吗?首先这个按钮“@drawable/normal_background”比这个按钮“@drawable/pressed_background”
    • 不只有一个带有@drawable/backgroundxml 的Button,其中backgroundxml 是您的xml 文件名,其中包含选择器。
    • 我试过了,但我不知道@drawable/normal_background 是什么。我的项目不能使用名为 normal_background 的资源
    • normal_background 只是您自己的图形的通配符。您需要将 normal_background 和 press_background 替换为您希望在按下按钮或正常时显示的可绘制对象。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      在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】:

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

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

        简化如下:

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

          <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】:

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

          【讨论】:

          • @Blackbelt 你在开玩笑吗?在 Pressed 和 UnPressed 状态下改变 View 的 bg 才是正确的方法!
          【解决方案6】:

          在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>
          

          比在你添加ImageButton的布局xml中设置背景如下:

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

          【讨论】:

          • 为什么会被否决?我想要这样的东西。但是,您的代码不起作用。我找到了这个帖子:stackoverflow.com/questions/1521640/… 与您的类似解决方案,但要长得多,现在尝试将其修剪下来。
          • @user3711421 我使用了相同的代码,它对我有用..您只是想在单击图像按钮时更改颜色!
          • 嗯,你还在用 android:src="path to the icon" 对吧?当我运行您提供的代码时,出现错误。 LogCat 没有给出具体建议。如果这可能是应用程序停止的原因,这些按钮位于片段内。
          • @user3711421 仍然没有找到您的问题的任何解决方案??
          • 我找到了它,但昨天无法发布,因为我的帐户是新帐户。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-07-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-29
          相关资源
          最近更新 更多