【问题标题】:Button with BOTH color and background image具有颜色和背景图像的按钮
【发布时间】:2012-05-15 12:53:13
【问题描述】:

有没有办法为按钮设置颜色背景图像?我想要一个带有透明 .png 的绿色按钮,但您似乎只能拥有一个。

【问题讨论】:

    标签: android xml button background background-image


    【解决方案1】:

    (不要)使用 ImageView

    将您的绿色按钮背景图像设置为背景,将透明 png 设置为 src。您可以将选择器应用到背景,就像应用到按钮一样...

    编辑:实际上已经为此制作了一个ImageButton,您应该改用它。让我一头雾水。

    【讨论】:

    • 谢谢,我使用的是Button 而不是ImageButton。现在可以了。我现在使用ImageButtonandroid:background 作为颜色,android:src 作为图像。
    【解决方案2】:

    试试这对我有用的代码:)

    <ImageButton
    android:id="@+id/button5"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ...
    android:src="@drawable/settingbtn"
    android:background="@drawable/red_color"
         />
    

    【讨论】:

      【解决方案3】:

      用透明度绘制单独的图像并保存图像。要为按下、聚焦等图像的实例显示不同的图像。使用以下方法。

      使用带有背景的图片按钮,如下图所示:

       `<?xml version="1.0" encoding="utf-8"?>
       <selector xmlns:android="http://schemas.android.com/apk/res/android">
       <item android:state_pressed="true"
             android:drawable="@drawable/imagewhenpressed" /> <!-- pressed -->
       <item android:state_focused="true"
             android:drawable="@drawable/imagewhenfocused" /> <!-- focused -->
       <item android:drawable="@drawable/defaultimage" /> <!-- default -->
       </selector>`
      

      注意:这里三个图片-imagewhenpressed.png,imagewhenfocused.png & defaultimage.png 和这个xml应该放在drawable目录下。 ImageButton 背景应该分配给这个 xml 的filename,比如android:background="@drawable/xmlfilename"

      【讨论】:

        【解决方案4】:

        请改用ImageButton

        您可以像正常一样设置背景,并在src 属性中选择图像。 如果图像变得太小或太大,您也可以弄乱scaleType 属性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-04-13
          • 2012-02-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-24
          • 1970-01-01
          相关资源
          最近更新 更多