实际编程中,系统提供的控件往往无法满足我们的需求,一来是样子丑陋,二来是一些复杂的组合需要多次使用的话,每次都写一堆控件的组合会很耗费时间,所以我们将这些组件的组合自定义为一个新的控件,以后使用的时候直接用该控件,方便又简单。最常见的例子就是软件中的titleTar

实现自定义控件的步骤:

1.设置控件的属性

2.实现我们的View

3.引用我们自定的View

  官方文档:http://developer.android.com/training/custom-views/create-view.html

下面开始:

设置控件的属性:

  分析一下上面四个titleBar,都是由一个居中的文本和一个右边一个图片按钮组成,只不过后面两个没有按钮而已

  那么我们自定义的这个组件就得包含一下几个基本属性:文本内容,文本大小,文本颜色,按钮路径

  在value中新建一个xml文件attrs.xml,把这些属性创建出来

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyCustomTitleBar">
<attr name="titleText" format="string"/>
<attr name="titleTextSize" format="dimension"/>
<attr name="titleTextColor" format="color"/>
<attr name="titleButtonImage" format="reference"/>
</declare-styleable>
</resources>
  declare-styleable标签是用来定义自定义控件的属性的,我们的控件属性都以attr形式存放在declare-styleable标签中,format是用来形容该属性的类型,有如下值可选:string,color,dimension,integer,enum,reference,float,boolean,fraction,flag

实现我们的View

定义一个继承自RelativeLayout的类MyCustomTitltBar,然后在他的构造方法中获取我们自定义的控件样式

package cn.lixyz.customviewdemo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView; @SuppressLint("NewApi")
public class MyCustomTitleBar extends RelativeLayout { // 定义自定义控件包含的组件
private TextView title;
private ImageButton button; // 定义控件的属性
private String titleText;
private float titleTextSize;
private int titleTextColor;
private Drawable titleButtonImage; // 为每个控件定义LayoutParams
private LayoutParams textLayoutParams;
private LayoutParams buttonLayoutParams; public MyCustomTitleBar(Context context, AttributeSet attrs) {
super(context, attrs); // 获取我们定义的属性
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyCustomTitleBar); titleText = array.getString(R.styleable.MyCustomTitleBar_titleText);
titleTextColor = array.getColor(R.styleable.MyCustomTitleBar_titleTextColor, 0);
titleTextSize = array.getDimension(R.styleable.MyCustomTitleBar_titleTextSize, 10);
titleButtonImage = array.getDrawable(R.styleable.MyCustomTitleBar_titleButtonImage); // 回收,以防出错
array.recycle(); // 新建包含的子组件
title = new TextView(context);
button = new ImageButton(context); // 为子组件赋值
title.setText(titleText);
title.setTextColor(titleTextColor);
title.setTextSize(titleTextSize);
button.setBackground(titleButtonImage); // 设置背景色
setBackgroundColor(0xFF38373c); // 设置包含控件的属性并添加到view中
textLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
addView(title, textLayoutParams);
buttonLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
addView(button, buttonLayoutParams);
}
}

   其中LayoutParams是用于child view(子视图) 向 parent view(父视图)传达自己的意愿的一个东西

  在获取属性的时候,android定义的格式为控件名_属性名  例如上面的MyCustomTitleBar_titleText、MyCustomTitleBar_titleTextSize

LayoutParams继承于Android.View.ViewGroup.LayoutParams. 
      LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。 
      可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。 
      但LayoutParams类也只是简单的描述了宽高,宽和高都可以设置成三种值: 
           1,一个确定的值; 
           2,FILL_PARENT,即填满(和父容器一样大小); 
           3,WRAP_CONTENT,即包裹住组件就好。

http://byandby.iteye.com/blog/816718

  TypedArray实例是个属性的容器,通过context.obtainStyledAttributes()方法返回得到

引用我们自定的View

在布局文件中引入我们自定义的控件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/cn.lixyz.customviewdemo"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="cn.lixyz.customviewdemo.MainActivity" > <cn.lixyz.customviewdemo.MyCustomTitleBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:titleButtonImage="@drawable/image"
custom:titleText="通讯录"
custom:titleTextColor="#ffffff"
custom:titleTextSize="5dp" >
</cn.lixyz.customviewdemo.MyCustomTitleBar> <cn.lixyz.customviewdemo.MyCustomTitleBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
custom:titleButtonImage="@drawable/plus"
custom:titleText="微信"
custom:titleTextColor="#ffffff"
custom:titleTextSize="5dp" >
</cn.lixyz.customviewdemo.MyCustomTitleBar> <cn.lixyz.customviewdemo.MyCustomTitleBar
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginTop="10dp"
custom:titleText="发现"
custom:titleTextColor="#ffffff"
custom:titleTextSize="5dp" >
</cn.lixyz.customviewdemo.MyCustomTitleBar> <cn.lixyz.customviewdemo.MyCustomTitleBar
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginTop="10dp"
custom:titleText="我"
custom:titleTextColor="#ffffff"
custom:titleTextSize="5dp" >
</cn.lixyz.customviewdemo.MyCustomTitleBar> </LinearLayout>

一定要在布局中引入我们的命名空间

  格式为:xmlns:自定义名称="http://schemas.android.com/apk/res/包名"
  因为只有引入了我们的命名空间,才可以使用xxx:xxx格式来设置我们定义的属性,我们平时使用的android:xx也是因为上面的xmlns:android="http://schemas.android.com/apk/res/android"
  这样运行一下软件查看效果

      为按钮设置点击事件

观察我们平时为按钮设置点击事件时候的方法:

bt.setOnClickListener(new View.OnClickListener() {

            @Override
public void onClick(View v) {
// TODO Auto-generated method stub }
});

我们通过按钮的setOnClickListener方法传入一个匿名内部类OnClickListener为按钮设置点击事件,我们查看OnClickListener的介绍

    /**
* Interface definition for a callback to be invoked when a view is clicked.
*/
public interface OnClickListener {
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
void onClick(View v);
}

  

我们也可以仿照这个形式,为我们的MyCustomTieleBar设置一个setCustomTitleBarListener,然后传入一个内部类来让这个按钮的点击调用这个类的click方法

修改MyCustomTitleBar:

package cn.lixyz.customviewdemo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView; @SuppressLint("NewApi")
public class MyCustomTitleBar extends RelativeLayout { // 定义自定义控件包含的组件
private TextView title;
private ImageButton button; // 定义控件的属性
private String titleText;
private float titleTextSize;
private int titleTextColor;
private Drawable titleButtonImage; // 为每个控件定义LayoutParams
private LayoutParams textLayoutParams;
private LayoutParams buttonLayoutParams; private customTitleBarListener listener; public interface customTitleBarListener {
public void click();
} public void setCustomTitleBarListener(customTitleBarListener listener) {
this.listener = listener;
} public MyCustomTitleBar(Context context, AttributeSet attrs) {
super(context, attrs); // 获取我们定义的属性
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyCustomTitleBar); titleText = array.getString(R.styleable.MyCustomTitleBar_titleText);
titleTextColor = array.getColor(R.styleable.MyCustomTitleBar_titleTextColor, 0);
titleTextSize = array.getDimension(R.styleable.MyCustomTitleBar_titleTextSize, 10);
titleButtonImage = array.getDrawable(R.styleable.MyCustomTitleBar_titleButtonImage); // 回收,以防出错
array.recycle(); // 新建包含的子组件
title = new TextView(context);
button = new ImageButton(context); // 为子组件赋值
title.setText(titleText);
title.setTextColor(titleTextColor);
title.setTextSize(titleTextSize);
button.setBackground(titleButtonImage); // 设置背景色
setBackgroundColor(0xFF38373c); // 设置包含控件的属性并添加到view中
textLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
addView(title, textLayoutParams);
buttonLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
addView(button, buttonLayoutParams); button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
listener.click();
}
}); }
}

  这样我们在MainActivity中就可以为按钮设置点击事件了

package cn.lixyz.customviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity { private MyCustomTitleBar addressListBar, wechatBar; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button bt = new Button(this);
bt.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub }
}); addressListBar = (MyCustomTitleBar) findViewById(R.id.addressListBar);
wechatBar = (MyCustomTitleBar) findViewById(R.id.wechatBar); addressListBar.setCustomTitleBarListener(new MyCustomTitleBar.customTitleBarListener() { @Override
public void click() {
Toast.makeText(MainActivity.this, "通讯录按钮被点击", Toast.LENGTH_SHORT).show();
}
});
wechatBar.setCustomTitleBarListener(new MyCustomTitleBar.customTitleBarListener() { @Override
public void click() {
Toast.makeText(MainActivity.this, "微信按钮被点击", Toast.LENGTH_SHORT).show();
}
}); } }

 

 

Android笔记(六十七) 自定义控件的更多相关文章

  1. Android笔记二十七.Service组件入门(一).什么是Service?

    转载请表明出处:http://blog.csdn.net/u012637501(嵌入式_小J的天空) 一.Service 1.Service简单介绍     Service为Android四大组件之中 ...

  2. Android笔记(六十九) 仿微信界面(一)

          综合之前的Fragment和自定义组件的知识,实现微信界面 MainActivity.java package cn.lixyz.test; import android.app.Acti ...

  3. Android笔记(六十八) Fragment总结

    Fragment的产生: 为了适应各种尺寸的屏幕,谷歌推出Fragment,可以把Fragment成Activity的一个组成部分,它拥有自己的生命周期.可以接收并处理用户的各种事件,还可以动态的增删 ...

  4. Android笔记(六十六) android中的动画——XML文件定义属性动画

    除了直接在java代码中定义动画之外,还可以使用xml文件定义动画,以便重用. 如果想要使用XML来编写动画,首先要在res目录下面新建一个animator文件夹,所有属性动画的XML文件都应该存放在 ...

  5. Android笔记(六十五) android中的动画——属性动画(propertyanimation)

    补间动画只能定义起始和结束两个帧在“透明度”.“旋转”.“倾斜”.“位移”4个方面的变化,逐帧动画也只能是播放多个图片,无法满足我们日常复杂的动画需求,所以谷歌在3.0开始,推出了属性动画(prope ...

  6. Android笔记(六十四) android中的动画——补间动画(tweened animation)

    补间动画就是只需要定义动画开始和结束的位置,动画中间的变化由系统去补齐. 补间动画由一下四种方式: 1.AplhaAnimation——透明度动画效果 2.ScaleAnimation ——缩放动画效 ...

  7. Android笔记(六十三) android中的动画——逐帧动画( frame-by-frame animation)

    就好像演电影一样,播放实现准备好的图片,来实现动画效果. 逐帧动画需要用到AnimationDrawable类,该类主要用于创建一个逐帧动画,然后我们把这个动画设置为view的背景即可. androi ...

  8. Android笔记(六十二)网络框架volley

    什么是Volley 很多时候,我们的APP都需要用到网络技术,使用HTTP协议来发送接收数据,谷歌推出了一个网络框架——volley,该框架适合进行数据量不大,但通信频繁的网络操作. 它的优点: (1 ...

  9. Android笔记(十七) Android中的Service

    定义和用途 Service是Android的四大组件之一,一直在后台运行,没有用户界面.Service组件通常用于为其他组件提供后台服务或者监控其他组件的运行状态,例如播放音乐.记录地理位置,监听用户 ...

随机推荐

  1. (4)Flask项目模板渲染初体验

    一.准备静态资源 将项目使用到的静态资源拷贝到static目录 二.创建前台首页html 创建templates/home/home.html页面,内容包含导航和底部版权两部分,中间内容区域为模板标签 ...

  2. jenkins报错Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password) 的处理

    问题背景:jenkins服务器发布代码后需要执行删除缓存的操作ssh -p222 eus_pe_devadmin@1.1.1.1 "sudo rm -rf /dev/shm/nginx/hi ...

  3. EasyNVR网页Chrome无插件播放安防摄像机视频流是怎么做到web浏览器延时一秒内

    背景说明 由于互联网的飞速发展,传统安防摄像头的视频监控直播与互联网直播相结合是大势所趋.传统安防的直播大多在一个局域网内,在播放的客户端上也是有所限制,一般都需要OCX Web插件进行直播.对于安防 ...

  4. docker 删除含有指定字符的container

    docker container ls -a|grep 指定字符 | awk '{print $1}'| xargs -I{} docker rm {}

  5. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  6. [LeetCode] 567. Permutation in String 字符串中的全排列

    Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...

  7. bat文件注释(jenkins中windows命令行中可以使用)

    1.BAT文件中如何注释:1.:: 注释内容(第一个冒号后也可以跟任何一个非字母数字的字符)2.rem 注释内容(不能出现重定向符号和管道符号)3.echo 注释内容(不能出现重定向符号和管道符号)〉 ...

  8. c#通过socket判断服务器连接是否正常

    判断Socket是否连接上,需要通过发包来确认. 之前确认都是调用调用socket的connected属性,然而该属性是上次的连接是否成功的结果,不及时. // 检查一个Socket是否可连接 pri ...

  9. idea关闭sonarLint自动扫描

    手动运行SonarLint 停止SonarLint自动检测代码之后,可以使用Ctrl+Shift+S手动运行SonarLint检查代码

  10. python断点

    pycharm怎么debug单步调试 1.打开一个Pycharm的界面,需要选中编辑器中的左侧. 2.点击Run---->Debug运行 3.点击箭头,向下运行 4.可以看到代码运行到下一条 5 ...