Android ImageButton 背景色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11623011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ImageButton background color
提问by Ayyoudy
I have an ImageButton with a background image that has some transparency. By default, the button gets a grey background where the transparency pixels are - due to the Holo.Light theme. I tried setting the background color of the button to transparent via the setBackgroundColor(Color.TRANSPARENT)
method. That works just fine and does what I need except now my button no longer has the light blue color when focused/pressed and looks rather flat (no borders around it, so it looks like an image).
我有一个带有一些透明度的背景图像的 ImageButton。默认情况下,由于 Holo.Light 主题,按钮在透明像素所在的位置获得灰色背景。我尝试通过该setBackgroundColor(Color.TRANSPARENT)
方法将按钮的背景颜色设置为透明。这工作得很好并且可以满足我的需要,除了现在我的按钮在聚焦/按下时不再具有浅蓝色并且看起来相当平坦(它周围没有边框,所以它看起来像一个图像)。
I googled and saw that you can assign selectors as explained herebut that would mean that I have to specify an image per button state and I don't want to do that. I want to inherit the focuses/pressed colors from the theme but overwrite the normal button background (when not pressed/focused) to transparent instead of grey. How can I achieve that?? Please provide a working example as I have tried many different combinations with no success.
我用谷歌搜索,看到你可以按照这里的解释分配选择器,但这意味着我必须为每个按钮状态指定一个图像,我不想这样做。我想从主题继承焦点/按下的颜色,但将普通按钮背景(未按下/聚焦时)覆盖为透明而不是灰色。我怎样才能做到这一点??请提供一个工作示例,因为我尝试了许多不同的组合但没有成功。
EditThank you all for helping. I figured out how to make this work without having to recreate the same image with the focused and pressed states for each button!
编辑谢谢大家的帮助。我想出了如何完成这项工作,而不必为每个按钮重新创建具有聚焦和按下状态的相同图像!
Here is my solution:
这是我的解决方案:
My button is defined as:
我的按钮定义为:
<ImageButton
android:id="@+id/ImageButton05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/button" />
And my background XML file (titled button.xml) is defined as follows:
我的背景 XML 文件(标题为 button.xml)定义如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item android:drawable="@drawable/btn_default_pressed_holo_light"></item>
<item android:drawable="@drawable/network_wifi"></item>
</layer-list>
</item>
<item android:state_focused="true">
<layer-list>
<item android:drawable="@drawable/btn_default_focused_holo_light"></item>
<item android:drawable="@drawable/network_wifi"></item>
</layer-list>
</item>
<item android:state_hovered="true">
<layer-list>
<item android:drawable="@drawable/btn_default_focused_holo_light"></item>
<item android:drawable="@drawable/network_wifi"></item>
</layer-list>
</item>
<item>
<layer-list>
<item android:drawable="@drawable/btn_default_normal_holo_light"></item>
<item android:drawable="@drawable/network_wifi"></item>
</layer-list>
</item>
</selector>
采纳答案by FoamyGuy
I want to inherit the focuses/pressed colors from the theme but overwrite the normal button background (when not pressed/focused) to transparent instead of grey. How can I achieve that??
我想从主题继承焦点/按下的颜色,但将普通按钮背景(未按下/聚焦时)覆盖为透明而不是灰色。我怎样才能做到这一点??
AFAIK you can't because the focus/pressed colors are built in to the same image resources that contain the grey background.
AFAIK 你不能,因为焦点/按下的颜色内置于包含灰色背景的相同图像资源中。
If you want to keep the system focus/pressed but remove the background you'll have to grab a copy of the image resources (which can be found in your SDK at /sdkroot/platforms/[version]/data/res/drawable-hdpi
replace [version] with whatever api level you are after. And if needbe replace hdpi with another density) and edit out the grey button from them with a photo editor. Then make a selector that references your modified images and set that as the background for your button.
如果您想保持系统焦点/按下但删除背景,您必须获取图像资源的副本(可以在您的 SDK 中找到,/sdkroot/platforms/[version]/data/res/drawable-hdpi
将 [version] 替换为您所追求的任何 api 级别。如果需要的话用另一种密度替换 hdpi)并使用照片编辑器从它们中编辑出灰色按钮。然后制作一个引用您修改过的图像的选择器,并将其设置为按钮的背景。
EDIT:Here are the default holo button images for focused and pressed
编辑:这是聚焦和按下的默认全息按钮图像
回答by Joanne
You can put something into an xml file, for example, custom_button.xml
and then set background="@drawable/custom_button"
in the button view.
例如,您可以将某些内容放入 xml 文件中,custom_button.xml
然后background="@drawable/custom_button"
在按钮视图中进行设置。
Please refer to this link as there is an xml example: Standard Android Button with a different color
请参考此链接,因为有一个 xml 示例:Standard Android Button with a different color
I used it in my code and it worked just the way I wanted.
我在我的代码中使用了它,它按照我想要的方式工作。
Reference: Standard Android Button with a different color
Edited:
编辑:
If you would rather use Maybe you should try to set a border. For example (Reference: Android - border for button):
如果您更愿意使用 Maybe 您应该尝试设置边框。例如(参考:Android - 按钮边框):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFF"
android:endColor="#00FF00"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
OR, You could try to set the style/theme for the button. (But it would be in a separate file) The style/theme contains the color attributes for various states of button such as focused / enabled / disabled/ etc.
或者,您可以尝试为按钮设置样式/主题。(但它会在一个单独的文件中)样式/主题包含按钮各种状态的颜色属性,例如聚焦/启用/禁用/等。
For setting background color/image and having click highlight effect, Here is an example (Reference: How to programmatically setting style attribute in a view)
用于设置背景颜色/图像并具有单击高亮效果,这是一个示例(参考:如何在视图中以编程方式设置样式属性)
<?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/btn_pressed" />
<item
android:state_pressed="false"
android:drawable="@drawable/btn_normal" />
</selector>
You can then apply this selector to a Button by setting the property android:background="@drawable/my_button".
然后,您可以通过设置属性 android:background="@drawable/my_button" 将此选择器应用于 Button。
回答by user2687744
You can use ImageView instead of ImageButton to solve your problem. Please set the android:background property of the ImageView to the background color of the parent.
您可以使用 ImageView 而不是 ImageButton 来解决您的问题。请将 ImageView 的 android:background 属性设置为父级的背景色。
回答by Kaspis245
You can write this in your xml file:
你可以在你的 xml 文件中写这个:
android:background="@null"
It worked for me.
它对我有用。
回答by Mohamed Ayed
if u don't need the default gray background and u need your ImageButton seems like an icon without no backgrounds just use android:background attribute and set it to
如果你不需要默认的灰色背景并且你需要你的 ImageButton 看起来像一个没有背景的图标,只需使用 android:background 属性并将其设置为
@android:color/transparent**
<ImageButton
android:id="@+id/ImageButton05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:background="@drawable/button" />
hope that help anybody
希望能帮助任何人