2013-03-31 114 views
3

我想更改图像按钮的背景。基本上,我在透明背景上看到的图像很棒,当有一大堆图像背景不透明时,图像就有点不舒服。仅更改ImageButton背景的一个状态(默认状态)

这应该是容易 - 只需上的按钮改变到android:background透明色(通过被拉伸): click_background.xml

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

    <item android:state_focused="true"><color android:color="#FF008080" /> 
    </item> 
    <item android:state_pressed="true"><color android:color="#FF008080" /> 
    </item> 
    <item><color android:color="#00000000" /> 
    </item> 

</selector> 

然后按钮

<ImageButton 
       android:id="@+id/players" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_gravity="center" 
       android:background="@drawable/click_background" 
       android:contentDescription="@string/players" 
       android:src="@drawable/speaker_tile" /> 

问题是我实际上想要保留state_focussedstate_pressed版本的背景(我认为它是从主题派生的)。当按下按钮或按下按钮时(通常显示的颜色与绘图完全相同),通常在按下按钮时主题将使用的按钮将会很有帮助。

我想知道如果有一种方法可以做到以下几点之一,但迄今无法找到任何东西:

  1. 定义一个选择,在某些东西从另一个继承(类似到主题可以从另一个主题继承的方式)。

  2. 创建一个全新的选择器,但它的XML引用是用于图像按钮的主题的state_focussedstate_pressed的颜色/可绘制。 编辑:它看起来像这个选项了。我会需要属性,但不能从drawable中引用它们。 See here

我想如果可能的话要做到这一点在声明XML,而不是编程的Java。

回答

3

您可以将Android OS使用的可绘制对象复制到您的项目中,并将它们用于状态列表drawable。您可以在{android-sdk-directory}/platforms/android-##/data/res/drawable[-*dpi]

编辑找到图片: 如果你去{android-sdk-directory}/platforms/android-##/data/res/values,你会发现由Android使用的themes.xmlstyles.xml文件。使用它们你可以找出你要找的哪些drawable。

例如,较新版本的Android的默认主题是Theme.Holo。这个主题有宣布像这样ImageButtons默认样式:

<item name="imageButtonStyle">@android:style/Widget.Holo.ImageButton</item>

styles.xml,这种风格被定义如下:

<style name="Widget.Holo.ImageButton" parent="Widget.ImageButton"> 
    <item name="android:background">@android:drawable/btn_default_holo_dark</item> 
</style> 

值得庆幸的background属性是正确的,在众目睽睽下定义。有时它是从父类型继承而来的,你必须找到它。在任何情况下,这里的绘制(这是因为它的XML的/drawable目录中找到):

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_window_focused="false" android:state_enabled="true" 
    android:drawable="@drawable/btn_default_normal_holo_dark" /> 
<item android:state_window_focused="false" android:state_enabled="false" 
    android:drawable="@drawable/btn_default_disabled_holo_dark" /> 
<item android:state_pressed="true" 
    android:drawable="@drawable/btn_default_pressed_holo_dark" /> 
<item android:state_focused="true" android:state_enabled="true" 
    android:drawable="@drawable/btn_default_focused_holo_dark" /> 
<item android:state_enabled="true" 
    android:drawable="@drawable/btn_default_normal_holo_dark" /> 
<item android:state_focused="true" 
    android:drawable="@drawable/btn_default_disabled_focused_holo_dark" /> 
<item 
    android:drawable="@drawable/btn_default_disabled_holo_dark" /> 
</selector> 

因此,这些都是Android使用了标准的ImageButton的默认(全息暗)主题背景的绘制资源。

+0

谢谢,任何提示,我将寻找绘图目录中的哪些项目?有很多需要搜索。 –

+0

我认为ImageButton使用与普通Button相同的背景。这也取决于什么平台级别。在较新版本的Android上,我认为btn_default_ {state} _holo_dark.9.png和btn_default_ {state} _holo_light.9.png是您正在寻找的那些,但我并不积极。通常,我必须查看平台资源中的res/values/styles.xml和res/values/styles.xml以确定我正在寻找的内容 – Karakuri

+0

此外,为什么我的答案被低估? – Karakuri