android控件---自定义带文本的ImageButton
由于SDK提供的ImageButton只能添加图片,不能添加文字;而Button控件添加的文字只能显示在图片内部;当我们需要添加文字在图片外部时就不能满足我们的需求了,顾只能自己写个自定义ImageButton。说是ImageButton,其实并不是继承于ImageButton,而是从LinearLayout继承,由于LinearLayout是线性排列,通过setOrientation(LinearLayout.VERTICAL)的方式达到View垂直排列的目的,所以很简单,只需要添加两个View:一个ImageView和一个TextView即可。代码如下:
package com.example.adtest; import android.content.Context;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; public class ImageButtonEx extends LinearLayout { private ImageView _imageView = null;
private TextView _textView = null; public ImageButtonEx(Context context){
super(context);
} public ImageButtonEx(Context context, int imageResId, int textResId) {
super(context);
// TODO Auto-generated constructor stub _imageView = new ImageView(context);
_textView = new TextView(context); _imageView.setImageResource(imageResId);
_textView.setText(textResId); _imageView.setPadding(0, 0, 0, 0);
_textView.setPadding(0, 0, 0, 0); setClickable(true);
setFocusable(true);
setBackgroundResource(android.R.drawable.btn_default);
setOrientation(LinearLayout.VERTICAL); addView(_imageView);
addView(_textView);
} public void setBtnText(CharSequence text)
{
_textView.setText(text);
} }
添加一个LinearLayout作为ImageButtonEx的容器。
<LinearLayout
android:id="@+id/llImageBtnEx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"/>
然后再Activity中添加调用代码:
package com.example.adtest; import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout; public class MainActivity extends ActionBarActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final ImageButtonEx imageBtnEx = new ImageButtonEx(this, R.drawable.icon, R.string.hello_world); imageBtnEx.setOnClickListener(new Button.OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
imageBtnEx.setBtnText("已经点击过了");
}}); LinearLayout llimageBtnEx = (LinearLayout)findViewById(R.id.llImageBtnEx);
llimageBtnEx.addView(imageBtnEx); } }

android控件---自定义带文本的ImageButton的更多相关文章
- Android控件_TextView(显示文本框控件)
一.TextView控件的常用属性 1.android:id——控件的id 2.android:layout_width——设置控件的宽度 wrap_content(包裹实际文本内容) fill ...
- android 控件自定义样式
一.按钮(Button) 方式1.存在.9图片或图片时 可在drawable文件夹下新建xml文件style_button_one.xml,代码如下 <?xml version=" ...
- 【Android开发日记】之入门篇(十四)——Button控件+自定义Button控件
好久不见,又是一个新的学期开始了,为什么我感觉好惆怅啊!这一周也发生了不少事情,节假日放了三天的假(好久没有这么悠闲过了),实习公司那边被组长半强制性的要求去解决一个后台登陆的问题,结果就是把 ...
- Android控件使用自定义字体
我们不可能只满足于系统自带的字体(太丑),其实控件自定义字体也很简单.. 1.首先找到该字体的ttf文件. 2.把字体文件放在scr/mian/assets/fonts下,如果没有该路径则自己创建. ...
- Android控件属性大全(转)
http://blog.csdn.net/pku_android/article/details/7365685 LinearLayout 线性布局 子元素任意: Tab ...
- Android群英传笔记——第三章:Android控件架构与自定义控件讲解
Android群英传笔记--第三章:Android控件架构与自定义控件讲解 真的很久没有更新博客了,三四天了吧,搬家干嘛的,心累,事件又很紧,抽时间把第三章大致的看完了,当然,我还是有一点View的基 ...
- Android入门(六):Android控件布局属性全解
第一类:属性值为true或falseandroid:layout_centerHrizontal 水平居中 (Hrizontal表示水平)android:layout_centerVertical 垂 ...
- UIAutomator定位Android控件的方法
UIAutomator各种控件定位的方法. 1. 背景 使用SDK自带的NotePad应用,尝试去获得在NotesList那个Activity里的Menu Options上面的那个Add note菜单 ...
- Android控件系列之RadioButton&RadioGroup(转)
学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...
随机推荐
- 纯js拖拽参考
function myDrag(obj){ obj.onmousedown=function(e){ var e=e||window.event; var diffX=e.clientX-this.o ...
- [Linux] Ubuntu Server 12.04 LTS 平台上搭建WordPress(Nginx+MySQL+PHP) Part IV
接下来我们去下载 WorePress 用最新的 3.7.1 下载地址是:http://cn.wordpress.org/wordpress-3.7.1-zh_CN.zip 我们先建立一个文件夹 /va ...
- linux 下su 和sudo 的用法以及区别
一. 使用 su 命令临时切换用户身份 1.su 的适用条件和威力 su命令就是切换用户的工具,怎么理解呢?比如我们以普通用户beinan登录的,但要添加用户任务,执行useradd ,beinan用 ...
- Android中内容观察者的使用---- ContentObserver类详解
详解:http://blog.csdn.net/qinjuning/article/details/7047607
- Adapter的getView方法详解
来自:http://blog.csdn.net/yelbosh/article/details/7831812 BaseAdapter就Android应用程序中经常用到的基础数据适配器,它的主要用途是 ...
- js中关于prototype学习(2015年1月5号晚)
prototype在js中为原型,只要是对象都有原型,最高原型为Object. 函数作为一特殊的对象,下面探讨prototype(原型)和function(函数)之间的关系. function A ( ...
- 利用HttpWebRequest访问WebApi
WebApi现在越来越流行,下面给出利用HttpWebRequest访问WebApi的工具方法: 1.利用基准URL和参数字典生成完整URL /// <summary> /// 生成URL ...
- Java4Android
变量 在计算机中存储信息需要声明变量的位置和所需的内存空间 boolean true false char ASCII字符集 计算机中所有数据都使用二进制表示 例如:a,b,c 适用七位二进制数进行表 ...
- Oracle 11gR2 RAC修改SCAN IP
一.查看当前环境: # grid用户 检查scan-ip地址的配置 [grid@node1 ~]$ srvctl config scan SCAN name: scan-cluster.com, Ne ...
- STM32F0xx_EXIT中断配置详细过程
Ⅰ.概述 EXIT外部中断在使用到按键或者开关控制等应用中比较常见,低功耗中断唤醒也是很常见的一种.因此,EXIT在实际项目开发中也是比较常见的一种. STM32F0中外部中断EXIT属于中断和事件的 ...