【问题标题】:Using Image in a Android Button with effects在带有效果的 Android 按钮中使用图像
【发布时间】:2013-06-17 10:43:41
【问题描述】:

(现在我在 StackOverflow 上遇到了相关问题,但不幸的是,没有一个解决方案对我有用,这就是为什么我不得不单独提出这个问题)

我是 Android 新手。问题:我需要一个充当按钮的图像。现在我明白了,这可以通过在标准按钮上使用图像或使用称为“ImageButton”的东西来实现(我想这是强烈推荐的,尽管我不知道为什么)。

要求:我需要详细的指导来解决这个问题。具体来说,我认为“将图像放在标准按钮上”更容易,直到我遇到两个主要问题:我无法将图像设置在中心(感谢 drawable-top、bottom、left6、right)以及一旦我更改背景颜色以匹配活动屏幕的背景颜色,按钮效果消失。简而言之,我需要一种适度简单的方法,让图像充当按钮或带有具有所有三种效果的图像的按钮:聚焦、按下和默认。我使用了 ImageButton 但后来我不知道如何制作自定义形状或 9patch 图像来给它所有想要的效果,我对 android 提供的默认按钮非常满意。我所需要的只是将背景悬停在图像上或类似的东西,以指示用户正在按下图像并且已生成事件!

有人可以帮我解决这个问题吗?我需要 UI 看起来不错,因此我的图像/按钮需要相应的效果。提前致谢:)

这是我的图标图像:

我希望对此有某种悬停效果,表明图像已像任何普通按钮一样被按下。

【问题讨论】:

标签: android button layout


【解决方案1】:

使用ImageButtonStateList Drawable。您需要针对不同按钮状态的选择器。您可以为不同的状态分配不同的drawable,以模仿普通按钮上的onFocus onPressed 效果。

这是drawable文件夹下res下的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:drawable="@color/cyan"/> <!-- pressed state -->
<item android:state_focused="true" 
      android:drawable="@color/cyan"/> <!-- focused state -->
<item android:drawable="@android:color/transparent"/> <!-- default state -->

</selector>

这是color.xmlvalues文件夹下的res

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <color name="cyan">#33B5E5</color>
</resources>

ImageButtonsrc设置为您的图像,并将背景设置为selector.xml

这是最终结果:

这里有很好的教程:Android ImageButton Selector Example

【讨论】:

  • 所以除了三个状态的三个不同的图像之外,我不可能使用android提供的默认动画吗?
  • @DarthCoder,什么样的动画?可绘制动画可用。如果您展示一些您想要的结果的图片,我认为我们可以提供进一步的帮助。
  • 我的意思是我喜欢android默认的蓝色(JellyBean)按钮按下时的颜色效果。默认情况下,该按钮看起来是深灰色,但可以使用 xml 进行更改。当我使用 drawable-top 将图像添加到按钮,然后将按钮的背景颜色更改为黑色以匹配活动背景时,我失去了按下按钮的效果。此外,我无法获取按钮中心的图像(尽管我可以使用 android:gravity="center" 获取 ImageButton 中心的图像)
  • 是的,类似的东西!!我的意思是图像周围的蓝色框略大。这可以做到吗?谢谢:)
  • 非常感谢@Sam Rad,太好了:)
【解决方案2】:

如果有人对此仍有疑问。 我创建了选择器,但将可绘制对象引用到两个不同的图像文件,并使用 imagebutton 中的 XML 作为源。它就像一个魅力。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_add_pressed"
        android:state_pressed="true" />
    <item android:drawable="@drawable/btn_add"
        android:state_focused="true" />
    <item android:drawable="@drawable/btn_add" />
</selector>

图片按钮如下所示:

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/add_button_selector"
        android:background="@null"/>

【讨论】:

    【解决方案3】:

    创建 xml 视图

    <ImageView
     android:id="@+id/imageview1"
     android:background="@drawable/selector_xml_name"
     android:layout_width="200dp"
     android:layout_height="126dp"/>
    

    在可绘制文件夹中创建 selector_xml_name.xml

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

    在可绘制文件夹 numpad_button_bg_selected.xml 内创建

     <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp">
     <solid android:color="@color/selected"/>
     <padding />
     <stroke android:color="#000" android:width="1dp"/>
     <corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      相关资源
      最近更新 更多