自定义按钮Custom Button

在本部分中,你将使用 " Button " 小组件和一个定义三个不同图像(用于不同按钮状态)的 XML 文件创建一个包含自定义图像(而不是文本)的按钮。In this section, you will create a button with a custom image instead of text, using the Button widget and an XML file that defines three different images to use for the different button states. 按下该按钮时,将显示一条短消息。When the button is pressed, a short message will be displayed.

右键单击并下载下面的三个映像,并将它们复制到项目的资源/可绘制目录。Right-click and download the three images below, then copy them to the Resources/drawable directory of your project. 这些状态将用于不同的按钮状态。These will be used for the different button states.

适用于处于焦点状态的黄色 android 图标橙色android 图标适用于按下状态的黄色 android图标Green Android icon for normal state Orange Android icon for focused state Yellow Android icon for pressed state

在名为 android_button 的Resources/ 目录中创建一个新文件。Create a new file in the Resources/drawable directory named android_button.xml. 插入以下 XML:Insert the following XML:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/android_pressed"
          android:state_pressed="true" />
    <item android:drawable="@drawable/android_focused"
          android:state_focused="true" />
    <item android:drawable="@drawable/android_normal" />
</selector>

这会定义一个可绘制的资源,该资源将基于按钮的当前状态更改其图像。This defines a single drawable resource, which will change its image based on the current state of the button. 第一个 <item> 在按下按钮时将android_pressed定义为图像(已激活);第二个 <item>android_focused定义为按钮焦点时的图像(使用轨迹球或方向面板突出显示按钮时);第三个 <item>android_normal定义为正常状态(未按下或未设定焦点时)的图像。The first <item> defines android_pressed.png as the image when the button is pressed (it's been activated); the second <item> defines android_focused.png as the image when the button is focused (when the button is highlighted using the trackball or directional pad); and the third <item> defines android_normal.png as the image for the normal state (when neither pressed nor focused). 此 XML 文件现在表示一个可绘制的资源,并在被其背景Button引用时,显示的图像将基于这三个状态更改。This XML file now represents a single drawable resource and when referenced by a Button for its background, the image displayed will change based on these three states.

备注

<item> 元素的顺序非常重要。The order of the <item> elements is important. 引用此可绘制时,按顺序遍历 <item>,以确定哪一项适合当前按钮状态。When this drawable is referenced, the <item>s are traversed in-order to determine which one is appropriate for the current button state. 由于 "normal" 图像是最后的,因此仅当 android:state_pressedandroid:state_focused 的条件都计算为 false 时才应用。Because the "normal" image is last, it is only applied when the conditions android:state_pressed and android:state_focused have both evaluated false.

打开Resources/layout/main.axml文件并添加Button元素:Open the Resources/layout/Main.axml file and add the Button element:

<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="@drawable/android_button" />

android:background 特性指定按钮背景使用的可绘制资源(在资源/可绘制/android .xml中保存时,将被引用为 @drawable/android)。The android:background attribute specifies the drawable resource to use for the button background (which, when saved at Resources/drawable/android.xml, is referenced as @drawable/android). 这会替换整个系统中用于按钮的正常背景图像。This replaces the normal background image used for buttons throughout the system. 为了使可绘制的基于按钮状态更改其图像,必须将图像应用到背景。In order for the drawable to change its image based on the button state, the image must be applied to the background.

若要使按钮在按下时执行操作,请在OnCreate()的末尾添加以下代码To make the button do something when pressed, add the following code at the end of the OnCreate() 付款方式method:

Button button = FindViewById<Button>(Resource.Id.button);

button.Click += (o, e) => {
    Toast.MakeText (this, "Beep Boop", ToastLength.Short).Show ();
};

这会从布局捕获Button ,然后添加在单击Button时要显示的Toast消息。This captures the Button from the layout, then adds a Toast message to be displayed when the Button is clicked.

现在运行该应用程序。Now run the application.

此页面的某些部分是基于 Android 开源项目创建和共享的工作的修改,并根据 创造性 Commons 2.5 归属许可证中所述的术语使用。Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.