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

时间: 2024-08-05 11:22:04

android控件---自定义带文本的ImageButton的相关文章

Android控件架构与自定义控件详解(二)——自定义View

在自定义View时,我们通常会去重写onDraw()方法来绘制View的显示内容.如果该View还需要使用wrap_content属性,那么还必须重写onMeasure()方法.另外,通过自定义attrs属性,还可以设置新的属性配置值. 在View中通常有一些比较重要的回调方法. onFinishInflate():从XML加载组件后回调. onSizeChanged(;:组件大小改变时. onMeasure():回调该方法来进行测量. onLayout():回调该方法来确定显示的位置. onT

Android控件上添加图片

项目中有一个点赞功能,点赞的小图标添加在点赞列表旁边,在xml里可以进行设置,也可以在代码中进行绘图. 下面是两种方法的设置: 1.xml里:一些控件:button.textView等等里面有个属性是android:drawableLeft 就可以将pic设置到text的左边.good.... 2.代码中: TextView txtlikedList = new TextView(this.getContext()); Drawable drawable= getResources().getD

UIAutomator定位Android控件的方法实践和建议(Appium姊妹篇)

在本人之前的一篇文章<<Appium基于安卓的各种FindElement的控件定位方法实践和建议>>第二章节谈到Appium可以通过使用UIAutomator的方法去定位Android界面上的控件,当时只是一笔带过举了个例子.如该文给自己的承诺,今天特撰写此文以描述UIAutomator各种控件定位的方法,以作为前文的姊妹篇互通有无. 1. 背景 为了和前文达成一致,这次的实践对象同样也是使用SDK自带的NotePad应用,同样是尝试去获得在NotesList那个Activity里

Android控件属性大全[整理转载]

控件属性: android属性 Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料, 第一类:属性值为true或falseandroid:layout_centerHrizontal??水平居中 (Hrizontal表示水平)android:layout_centerVertical???垂直居中 (Vertiacl表示垂直)android:layout_centerInparent????相对于父元素完全居中android:layout_alig

Robotium之Android控件定位实践和建议(Appium/UIAutomator姊妹篇)

本人之前曾经撰文描述Appium和UIAutomator框架是如何定位Android界面上的控件的. UIAutomator定位Android控件的方法实践和建议 Appium基于安卓的各种FindElement的控件定位方法实践和建议 今天我们换一个渊源更留长,当今更盛行的框架Robotium,实践下看它又是如何对控件进行定位的. 1. 背景 为保持这个系列的一致性,我们继续用SDK自带的NotePad实例应用作为我们的试验目标应用,但是这次不仅仅是像以前一样主要围绕Menu Option里面

Android控件系列之RadioButton&amp;RadioGroup

学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握RadioGroup选中状态变换的事件(监听器) RadioButton和CheckBox的区别: 1.单个RadioButton在选中后,通过点击无法变为未选中 单个CheckBox在选中后,通过点击可以变为未选中 2.一组RadioButton,只能同时选中一个 一组CheckBox,能同时选中多个

Android控件系列之RadioButton&amp;RadioGroup 【转】

学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握RadioGroup选中状态变换的事件(监听器) RadioButton和CheckBox的区别: 1.单个RadioButton在选中后,通过点击无法变为未选中 单个CheckBox在选中后,通过点击可以变为未选中 2.一组RadioButton,只能同时选中一个 一组CheckBox,能同时选中多个

Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现

Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现 2015-03-10 22:38 28419人阅读 评论(17) 收藏 举报  分类: Android UI(819)  Android开发(1568)  关注finddreams:http://blog.csdn.net/finddreams/article/details/43486527 今天我们来模仿一下支付宝钱包首页中带有分割线的GridView,俗称九宫格.先上图,是你想要的效果么?如果

【转】UIAutomator定位Android控件的方法实践和建议(Appium姊妹篇)

原文地址:http://blog.csdn.net/zhubaitian/article/details/39777951 在本人之前的一篇文章<<Appium基于安卓的各种FindElement的控件定位方法实践和建议>>第二章节谈到Appium可以通过使用UIAutomator的方法去定位Android界面上的控件,当时只是一笔带过举了个例子.如该文给自己的承诺,今天特撰写此文以描述UIAutomator各种控件定位的方法,以作为前文的姊妹篇互通有无. 1. 背景 为了和前文达