自定义带图片和文字的ImageTextButton

时间:2022-09-17 15:11:40

今天我们来讲一下有关自定义控件的问题,今天讲的这篇是从布局自定义开始的,难度不大,一看就明白,估计有的同学或者开发者看了说,这种方式多此一举,但是小编我不这么认为,多一种解决方式,就多一种举一反三的学习。下一次或者过几天我会从自定义属性,在布局文件中使用属性的方式再讲一篇关于自定义控件的文章,希望对大家能够有所帮助。





现在开始讲自定义带图片和文字的ImageTextButton的实现方法。

效果图如下:

自定义带图片和文字的ImageTextButton

第一步:新建一个image_text_buttton.xml的布局文件,供自定义的控件使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" > <ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="12dp" /> <TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:textColor="#000000" /> </LinearLayout>

第二步:自定义一个类ImageTextButton继承LinearLayout

package net.loonggg.itbd.view;

import net.loonggg.itbd.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; public class ImageTextButton extends LinearLayout {
private ImageView iv;
private TextView tv; public ImageTextButton(Context context) {
super(context);
} public ImageTextButton(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.image_text_buttton, this,
true);
iv = (ImageView) findViewById(R.id.iv);
tv = (TextView) findViewById(R.id.tv);
} public void setDefaultImageResource(int resId) {
iv.setImageResource(resId);
} public void setDefaultTextViewText(String text) {
tv.setText(text);
} /**
* @param resId
*/
public void setImageResource(int resId) {
iv.setImageResource(resId);
} /**
* @param text
*/
public void setTextViewText(String text) {
tv.setText(text);
} /**
* @param color
*/
public void setTextColor(int color) {
tv.setTextColor(color);
} }

第三步:自定义控件的使用,在布局文件activity_main.xml中的使用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <net.loonggg.itbd.view.ImageTextButton
android:id="@+id/itb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </RelativeLayout>

第四步:在Activiy中的使用

public class MainActivity extends Activity {
private ImageTextButton itb; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); itb = (ImageTextButton) findViewById(R.id.itb);
itb.setImageResource(R.drawable.sure);
itb.setTextViewText("确定");
} }

到这里就讲完了,其实看完代码就知道了,非常简单。大家可以试一试哦!获取demo的方法跟以前一样,只需在公众号里回复关键字“2”即可获得。

微信公众号:smart_android ,公众号[非著名程序员]可能是东半球最好的技术分享公众号。每天,每周定时推送一些有关移动开发的原创文章和教程。

自定义带图片和文字的ImageTextButton的更多相关文章

  1. 自定义带图片和文字的Button的排版--陈棚

    自定义button,动态加载图片与文字 [footView addSubview:btnAllChoose]; [btnAllChoose setTitle:str forState:UIContro ...

  2. tableView左划自定义带图片按钮

    本方法实现的原理是将自定义按钮加在tableViewCell.contentView的屏幕外的frame上,打个比方,如果是5系的话,那么你自定义按钮的frame的起点就在(320+,0)(320+表 ...

  3. 基于bootstrap的轮播广告页,带图片和文字

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...

  4. Android 自定义Android带图片和文字的ImageButton

    经过分析,上述按钮效果实际上就是一个布局,一个最简单不过的垂直线性布局,上部分是一个ImageView,下部分是一个TextView,这个布局可点击.可设置监听. 我们首先要编写自己的ImageBut ...

  5. iOS 自定义UIButton&lpar;图片和文字混合&rpar;

    // UIApplicationDelegate  .h文件 #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder &l ...

  6. 自定义组件---图片和文字实现ImageButton效果

    1.效果图 2.自定义代码: <span style="font-family:Comic Sans MS;font-size:14px;">public class ...

  7. 【Wince-自定义控件】ImageButton 带图片、文字

    1.看图 可以实现MouseDown改变背景颜色或背景图片. 遗憾是没有实现键盘触发按钮事件. 2.选择继承自Control基类 public class ImageButton : Control ...

  8. JAVA实现带图片的列表——JList

    JList:显示对象列表并且允许用户选择一个或多个项的组件. JList的构造方法: 1.根据数组创建列表: JList(Object[] listData) 构造一个 JList,使其显示指定数组中 ...

  9. Android实现自定义带文字和图片的Button

    Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...

随机推荐

  1. VisualSFM for Structure from Motion

    VisualSFM是Changchang Wu编写的使用 Structure from Motion (SfM)进行3D重建的交互界面,具体内容详见http://homes.cs.washington ...

  2. Eclipse下Ruby的配置&rsqb;

      简述: 在Eclipse中开发Ruby开发环境   步骤: 第一步, 1. 在Eclipse的Help ->  Install New Software输入 http://download. ...

  3. 【众秒之门 JavaScript与jQuery技术精粹 &num;BOOK&num;】第1章 初学JavaScript需知的七件事

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  4. Nutch&plus;Hadoop集群搭建

    转载自:http://www.open-open.com/lib/view/open1328670771405.html 1.Apache Nutch    Apache Nutch是一个用于网络搜索 ...

  5. openstack中文文档

    http://www.openstack.cn/p392.html   openStack Hacker中文文档 http://docs.mirantis.com/fuel-dev/develop/a ...

  6. java多线程系列&lpar;二&rpar;

    对象变量的并发访问 前言:本系列将从零开始讲解java多线程相关的技术,内容参考于<java多线程核心技术>与<java并发编程实战>等相关资料,希望站在巨人的肩膀上,再通过我 ...

  7. 如何用VS EF连接 Mysql,以及执行SQL语句 和存储过程?

    VS2013, MySQL5.7.18 , MySQL5.7.14 执行SQL语句: ztp_user z = new ztp_user(); object[] obj = new object[] ...

  8. &lbrack;bx&rsqb;和loop指令

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  9. SQL字符串操作汇总

    SQL字符串操作汇总 --将字符串中从某个字符开始截取一段字符,然后将另外一个字符串插入此处 select stuff('hello,world!',4,4,'****')   --返回值hel*** ...

  10. python3&period;4学习笔记&lpar;三&rpar; idle 清屏扩展插件

    python3.4学习笔记(三) idle 清屏扩展插件python idle 清屏问题的解决,使用python idle都会遇到一个常见而又懊恼的问题——要怎么清屏?在*看到 ...