更多精彩内容,请点击阅读:《API Demos 2.3 学习笔记》
下面我们以ImageButton为例,简单介绍如何引用android系统内置图标资源。
1、在java代码中引用
在java代码中,我们通过“android.R.drawable.图标名称”格式来引用系统图标资源。具体参考如下:
private ImageButton mImageButton = null;
//通过findViewById获得ImageButton
mImageButton = (ImageButton)findViewById(R.id.myImageButton01);
//引用android内置图标,作为ImageButton的按钮图标
mImageButton.setImageResource(android.R.drawable.sym_action_call);
2、在xml布局文件中 在布局文件中,我们通常按照"@android:drawable/图标名称"格式来引用资源。
具体参考如下:
<ImageButton
android:id="@+id/myImageButton01"
android:layout_width="100dip"
android:layout_height="50dip"
android:src="@android:drawable/sym_action_call"/>
注:我们可以在AndroidSDK目录下找到这些系统内置图标资源,具体位置在对应Android版本的资源目录下。以android2.3为例,这些图标在android-sdk-linux_86/platforms/android-9/data/res/drawable-hdpi目录下。
另外,除了引用android系统内置图标之外,我们也可以引用自定义图标。具体操作如下:
1、为了表示按钮不同状态(例如:被聚焦,被点击等),我们可以为每种状态定义一张图片。首先,我们准备三张类似的图片,放在drawable目录下。
-
-
button_normal
button_pressed
button_focused
-
2、在drawable目录下新建一个xml文件btn_star.xml,通过"selector"来定义正常状态,聚焦状态下以及按下状态下分别显示什么图标。
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
3、以在xml布局文件中引用为例,在引用该自定义图标时,图标名称为定义在drawable下的xml文件名称(不包括xml后缀)。例如,上面我们定义了btn_star.xml,引用时,可以这样引用:"@android:drawable/btn_star"。
<ImageButton
android:id="@+id/myImageButton04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/btn_star"/>
下面我们进行实例代码解析:
res-layout-image_button_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- ImageButton,引用android系统内置图标sym_action_call(拨打电话)作为按钮图标 -->
<ImageButton
android:id="@+id/myImageButton01"
android:layout_width="100dip"
android:layout_height="50dip"
android:src="@android:drawable/sym_action_call" />
<!-- ImageButton,引用android系统内置图标sym_action_chat(聊天)作为按钮图标 -->
<ImageButton
android:id="@+id/myImageButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/sym_action_chat" />
<!-- ImageButton,引用android系统内置图标sym_action_email(邮件)作为按钮图标 -->
<ImageButton
android:id="@+id/myImageButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/sym_action_email" />
<!-- ImageButton,引用自定义图标作为按钮图标 -->
<ImageButton
android:id="@+id/myImageButton04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/btn_star" />
</LinearLayout>
src-com.example.android.apis.view-ImageButton1.java
package com.example.android.apis.view;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import com.example.android.apis.R;
public class ImageButton1 extends Activity {
private ImageButton mImageButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_button_1);
//通过findViewById获得ImageButton
mImageButton = (ImageButton)findViewById(R.id.myImageButton01);
//引用android内置图标,作为ImageButton的按钮图标
mImageButton.setImageResource(android.R.drawable.sym_action_call);
}
}
预览效果:
欢迎大家关注我的微信公众号:
微信公众号:sn0wdr1am