API Demos 2.3 学习笔记 (8)-- Views->ImageButton

更多精彩内容,请点击阅读:《API Demos 2.3 学习笔记》


ImageButton示例介绍了定义ImageButton的基本方法。从示例布局文件来看,主要是在线性LinearLayout中定义三个ImageButton。不同的是,这三个按钮通过android:src属性分别引用三张来自android系统内置的图片作为按钮图标。
下面我们以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);

    }
}

预览效果:

时间: 2024-07-30 20:07:41

API Demos 2.3 学习笔记 (8)-- Views-&gt;ImageButton的相关文章

API Demos 2.3 学习笔记 (9)-- Views-&amp;gt;Visibility

更多精彩内容,请点击阅读:<API Demos 2.3 学习笔记> Visibility 示例以TextView为例介绍了View的三种可见性以及如何设置View的可见性.这些可见性的设置方法同样适用于View以及其他继承自View的子类对象.从示例布局文件来看,主要分为两部分,一部分为一个线性垂直布局,包含三个不同背景色的TextVew对象:另一部分,为一个线性水平布局,包含三个Button对象. View的可见性主要分为三种, VISIBLE(可见). INVISIBLE(不可见). GO

API Demos 2.3 学习笔记 (11)-- Views-&amp;gt;Date Widgets

更多精彩内容,请点击阅读:<API Demos 2.3 学习笔记> Date Widgets大致可以分为两类,一类是弹出对话框类型的控件,包括DatePickerDialog(日期选择对话框控件)和TimePickerDialog(时间选择对话框控件):另一类就是内嵌类型的控件,包括DatePicker(日期选择窗口控件)和TimePicker(时间选择窗口控件).这些控件广泛应用在需要选择和记录时间信息的场合,例如:谷歌日历. 下面我们简单介绍下这四种控件的调用方法: 1.DatePicke

API Demos 2.3 学习笔记 (17)-- Views-&amp;gt;Tabs

更多精彩内容,请点击阅读:<API Demos 2.3 学习笔记> Tab与TabHost应用很广泛.打开android手机的默认电话拨号程序,上面就是由"拨号","通话记录","通讯录"以及"收藏"四个选项卡组成的. TabHost有两种实现方式,一种是继承TabActivity,另一种是自己定义TabHost,不继承TabActivity.APIDemo中的三个实例都是第一种.想了解TabHost的第二种实现方

API Demos 2.3 学习笔记

API Demos 2.3 学习笔记 Doc和Pdf版本同步更新,请使用git工具进行同步.关于Git工具更多信息,请参考:http://progit.org/book/zh/ git clone https://code.google.com/p/apidemo/  或者 git clone git://github.com/snowdream/apidemo.git 由于笔者才疏识浅.时间匆忙,书中难免出现错误.缺点,欢迎大家批评指正.联系方式:yanghui1986527#gmail.co

API Demos 2.3 学习笔记 (19)-- Views-&amp;gt; TextSwitcher

更多精彩内容,请点击阅读:<API Demos 2.3 学习笔记> TextSwitcher即文字交换器,是只能包含TextView作为唯一子类型的控件,常用于屏幕上文字标签的动画切换.每次调用setText(CharSequence),TextSwitcher会将当前文字用预定淡出动画隐藏,而将最新文字用预订淡入动画显示出来. 下面简单介绍怎么创建和使用TextSwitcher控件. 1.首先,在布局文件中定义一个TextSwitcher控件 <TextSwitcher android

API Demos 2.3 学习笔记 (15)-- Views-&amp;gt;Radio Group

更多精彩内容,请点击阅读:<API Demos 2.3 学习笔记> 想想我们上学时候做的单项选择题,其中只有一个是正确答案.在做题的时候,我们只能选择一项.如果我们想在Android上设计一道单项选择题的话,可能就要用到RadioGroup了.RadioGroup常常和RadioButton一起使用.由一个RadioGroup包含若干个RadioButton,组成一个单项选择群组.我们在同一时间只能选中该组中的一个 RadioButton. RadioGroup的创建主要有两种方法: 1.在x

API Demos 2.3 学习笔记 (10)-- Views-&amp;gt;WebView

更多精彩内容,请点击阅读:<API Demos 2.3 学习笔记> WebView是一个专门用来显示网页的View子类.它使用WebKit渲染引擎来显示网页,并且支持包括前进,后退,放大,缩小,文本搜索等多种功能. WebView有一个辅助类叫WebSettings,它管理WebView的设置状态.该类的对象可以通过WebView.getSettings()方法获得.下面我们介绍几个常用的WebSettings设置: //得到WebView对象 WebView mWebView = (WebV

API Demos 2.3 学习笔记 (13)-- Views-&amp;gt;Seek Bar

更多精彩内容,请点击阅读:<API Demos 2.3 学习笔记> SeekBar 是基于ProgressBar的扩展,可以理解为添加了滑动条的ProgressBar.用户可以左右移动滑动条或者左右移动轨迹球来设置当前的进度值.最好不要在SeekBar左边或者右边放置一个可以聚焦的控件. SeekBar 控件最经典的应用是在播放器中用于显示/改变播放进度的进度条.下面是一个简单的SeekBar 控件: <SeekBar android:id="@+id/seek" a

API Demos 2.3 学习笔记 (7)-- Views-&amp;gt;Buttons

更多精彩内容,请点击阅读:<API Demos 2.3 学习笔记> Buttons示例介绍了定义Button,ToggleButton的基本方法.从示例布局文件来看,主要是在线性LinearLayout中定义三个Button,其中第一个为正常的Button,而第二个通过style属性定义了一个小的Button,第三个为ToggleButton. 这里我们主要介绍下ToggleButton.这是一种具有选中和未选中两种状态的按钮,类似开关按钮.通过"android:textOn&quo

API Demos 2.3 学习笔记 (12)-- Views-&amp;gt;Rating Bar

更多精彩内容,请点击阅读:<API Demos 2.3 学习笔记> RatingBar是基于SeekBar和ProgressBar的扩展,用星型来显示等级评定.用户可以通过触屏点击或者轨迹球左右移动来进行星型等级评定.RatingBar有三种风格:ratingBarStyle 默认风格  ratingBarStyleSmall 小风格 ratingBarStyleIndicator 大风格 其中,默认风格的RatingBar是我们通常使用的可以交互的,而后面两种不能进行进行交互,只能作为指示牌