public class ImageButton
extendsImageView
java.lang.Object | |||
↳ | android.view.View | ||
↳ | android.widget.ImageView | ||
↳ | android.widget.ImageButton |
Displays a button with an image (instead of text) that can be pressed or clicked by the user. By default, an ImageButton looks like a regularButton
, with the standard button background that changes color during different button states. The image on the surface of the button is defined either by the android:src
attribute in the
XML element or by thesetImageResource(int)
method.
在按钮上显示图像(而不是文本),它可以被用户按下或点击。默认情况下,一个ImageButton看起来就像一个普通Button
,标准按钮的背景在不同的按钮状态下,会改变颜色。按钮上的图片,由
XML元素中android:src
属性来定义,或由setImageResource(int)
方法来设置。
To remove the standard button background image, define your own background image or set the background color to be transparent.
删除标准按钮上的背景图片,定义你自己的背景图片或者设置背景颜色为透明。
To indicate the different button states (focused, selected, etc.), you can define a different image for each state. E.g., a blue image by default, an orange one for when focused, and a yellow one for when pressed. An easy way to do this is with an XML drawable "selector." For example:
指明不同的按钮状态(focused,selected,等),你可以为每个状态定义不同的图片。例如,默认是一个蓝色的图片,当focused状态时,则为一个橙色的图片,而当pressed状态时,则为一个黄色的图片。一种简单的方法是使用一个XML drawable的selector”。例如:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android"> - android:state_pressed="true" android:drawable="@drawable/button_pressed" />
- android:state_focused="true" android:drawable="@drawable/button_focused" />
- android:drawable="@drawable/button_normal" />
Save the XML file in your project res/drawable/
folder and then reference it as a drawable for the source of your ImageButton (in theandroid:src
attribute). Android will automatically change the image based on the state of the button and the corresponding images defined in the XML.
将XML文件保存到你的项目中的res/drawable/
路径下,然后作为你的ImageButton的源的一个drawable来引用(在android:src
属性中)。Android将根据按钮的状态和XML中定义的相应的图像,自动更改按钮的图像。
The order of the
elements is important because they are evaluated in order. This is why the "normal" button image comes last, because it will only be applied afterandroid:state_pressed
and android:state_focused
have both evaluated false.
元素中的顺序非常重要,因为它们是按顺序进行计算的。这就是为什么“normal”的按钮图片是放置最后,因为它只会在android:state_pressed
和android:state_focused
都被评估为false时,才会使用。
See the Buttons guide.
请参考Buttons。
XML attributes
XML属性
See Button Attributes
,View Attributes
参考Button Attributes
View Attributes
继承XML属性 |
|
---|---|
From class android.widget.ImageView
|
|
From class android.view.View
|
继承常量 |
---|
From class android.view.View
|
继承字段 |
---|
From class android.view.View
|
Public构造方法 |
|
---|---|
ImageButton(Context context) |
|
ImageButton(Context context, AttributeSet attrs) |
|
ImageButton(Context context, AttributeSet attrs, int defStyleAttr) |
|
ImageButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) |
Public方法 |
|
---|---|
CharSequence |
getAccessibilityClassName() Return the class name of this object to be used for accessibility purposes. |
PointerIcon |
onResolvePointerIcon(MotionEvent event, int pointerIndex) Returns the pointer icon for the motion event, or null if it doesn't specify the icon. |
保护方法 |
|
---|---|
boolean |
onSetAlpha(int alpha) Invoked if there is a Transform that involves alpha. |
继承方法 |
|
---|---|
From class android.widget.ImageView
|
|
From class android.view.View
|
|
From class java.lang.Object
|
|
From interface android.graphics.drawable.Drawable.Callback
|
|
From interface android.view.KeyEvent.Callback
|
|
From interface android.view.accessibility.AccessibilityEventSource
|
ImageButton (Context context)
参数 | |
---|---|
context |
Context |
ImageButton (Context context, AttributeSet attrs)
参数 | |
---|---|
context |
Context |
attrs |
AttributeSet |
ImageButton (Context context, AttributeSet attrs, int defStyleAttr)
参数 | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
ImageButton (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
参数 | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
defStyleRes |
int |
CharSequence getAccessibilityClassName ()
Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName
.
返回此对象的类名,用于辅助功能目的。如果它们实现了一些在辅助服务中被视为视图的一个完全的新类,而与它所派生的类无关,则子类应该仅重载此方法。这个值在AccessibilityNodeInfo.setClassName
中被填充。
返回 | |
---|---|
CharSequence |
完整内容请查看http://www.zhdoc.net/android/reference/android/widget/ImageButton.html