2013-12-18 11:25:22

转载自: http://www.open-open.com/lib/view/open1328836804515.html

很多时候android常用的控件不能满足我们的需求,那么我们就需要自定义一个控件了。今天做了一个自定义控件的实例,来分享下。

首先定义一个layout实现按钮内部布局:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" > <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="40dip"
android:paddingTop="5dip"
android:src="@drawable/right_icon" /> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dip"
android:text="确定"
android:textColor="#000000" /> </LinearLayout>

接下来写一个类继承LinearLayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。

 public class ImageBtn extends LinearLayout {

     private ImageView imageView;
private TextView textView; public ImageBtn(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ImageBtn(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.imagebtn, this);
imageView=(ImageView) findViewById(R.id.imageView1);
textView=(TextView)findViewById(R.id.textView1);
} /**
* 设置图片资源
*/
public void setImageResource(int resId) {
imageView.setImageResource(resId);
} /**
* 设置显示的文字
*/
public void setTextViewText(String text) {
textView.setText(text);
} }

在需要使用这个自定义控件的layout中加入这控件,只需要在xml中加入即可。

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" > <cn.com.karl.view.ImageBtn
android:id="@+id/btn_right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn"
/> <cn.com.karl.view.ImageBtn
android:id="@+id/btn_error"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn"
/> </LinearLayout>

这里用到了背景图片 在drawable/btn.xml:

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/btn_normal"></item>
<item android:state_pressed="true" android:drawable="@drawable/btn_white"></item>
<item android:state_checked="true" android:drawable="@drawable/btn_white"></item>
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/btn_normal"></item> </selector>

最后在activity中设置该控件,和其他控件差不多:

 public class IdentifyButtonActivity extends Activity {
private ImageBtn imageBtn1;
private ImageBtn imageBtn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.identifybutton); imageBtn1=(ImageBtn) this.findViewById(R.id.btn_right);
imageBtn2=(ImageBtn) this.findViewById(R.id.btn_error);
imageBtn1.setTextViewText("确定");
imageBtn2.setTextViewText("取消");
imageBtn1.setImageResource(R.drawable.right_icon);
imageBtn2.setImageResource(R.drawable.error_icon); imageBtn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "点击的正确按钮", 1).show();
}
}); imageBtn2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "点击的错误按钮", 1).show();
}
});
}
}

最后看看我们自定义控件的效果吧!

点击后还有按下按钮的效果。

android自定义控件实例(Linearlayout组合TextView和ImageView)的更多相关文章

  1. Android自定义控件之自定义组合控件

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

  2. android自定义控件实例

    很多时候android常用的控件不能满足我们的需求,那么我们就需要自定义一个控件了.今天做了一个自定义控件的实例,来分享下. 首先定义一个layout实现按钮内部布局: 01 <?xml ver ...

  3. android自定义控件(五) 自定义组合控件

    转自http://www.cnblogs.com/hdjjun/archive/2011/10/12/2209467.html 代码为自己编写 目标:实现textview和ImageButton组合, ...

  4. Android自定义控件之自定义组合控件(三)

    前言: 前两篇介绍了自定义控件的基础原理Android自定义控件之基本原理(一).自定义属性Android自定义控件之自定义属性(二).今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发 ...

  5. Android自定义控件实例,圆形头像(图库 + 裁剪+设置),上传头像显示为圆形,附源码

    Android项目开发中经常会遇见需要实现圆角或者圆形的图片功能,如果仅仅使用系统自带的ImageView控件显然无法实现此功能,所以通过系列文章的形式由简到繁全方位的介绍一下此功能的实现,巩固一下自 ...

  6. Android自定义控件 -- 带边框的TextView

    使用xml实现边框 原来使用带边框的TextView时一般都是用XML定义来完成,在drawable目录中定义如下所示的xml文件: <?xml version="1.0" ...

  7. Android自定义控件之基本原理

    前言: 在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件,今天先大致了解一下自定义控件的要求和实现的基本原理. 自 ...

  8. Android自定义控件之自定义ViewGroup实现标签云

    前言: 前面几篇讲了自定义控件绘制原理Android自定义控件之基本原理(一),自定义属性Android自定义控件之自定义属性(二),自定义组合控件Android自定义控件之自定义组合控件(三),常言 ...

  9. Android自定义控件之自定义属性

    前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...

随机推荐

  1. WPA/WPA2四次握手

    WPA/WPA2四次握手 官方文档:https://en.wikipedia.org/wiki/IEEE_802.11i-2004 The four-way handshake is designed ...

  2. 如何查看与刷新DNS本地缓存

    如何查看与刷新DNS本地缓存 一.查看DNS本地缓存 在cmd窗口输入:ipconfig/displaydns 二.刷新DNS本地缓存 在cmd窗口输入:ipconfig/flushdns 之后输入: ...

  3. UEditor 之查询当前编辑区域的状态是源码模式还是可视化模式

    在使用百度的编辑器的时候,遇到了这样的一个问题: 解决方法是 使用了两个命令:

  4. require 和 file_get_contents

    requirerequire_onceincludeinclude_oncecurlfile_get_contents---各种选择的比较 还有这么复杂的说法,怎么办? 在开发过程中发现,用requi ...

  5. The Daligner Overlap Library

    /************************************************************************************\ * * * Copyrig ...

  6. retrifit

    Retrofit 特点 性能最好,处理最快 使用REST API时非常方便: 传输层默认就使用OkHttp: 支持NIO: 拥有出色的API文档和社区支持 速度上比volley更快: 如果你的应用程序 ...

  7. css制作简单的导航栏

    //css代码 #menu{ height: 65px; width:100%; background-color: rgba(0, 0, 0, 0.5);}#menu ul{ list-style: ...

  8. Docker 使用指南 (六)—— 使用 Docker 部署 Django 容器栈

    版权声明:本文由田飞雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/98 来源:腾云阁 https://www.qclou ...

  9. css读书笔记1:HTML标记和文档结构

    块级元素和行内元素:块级元素:上下堆叠,每个块级元素都独立占一行.块级元素的盒子宽度与父元素同宽.行内元素:左右堆叠,只有在空间不足的情况下才会折到下一行显示.行内元素的盒子会收缩包裹其内容,并尽可能 ...

  10. Mustache.js使用笔记(内容属于转载总结)

    1.Mustache的语法很简单,用两个大括号标记要绑定的字段即可,“{{}}” Mustache主要的渲染方法为Mustache.render(),该方法有两个参数,第一个为要渲染的模版, 也就是例 ...