Google 公司为我们开发Android应用程序提供了丰富的UI组件,有时一个控件无法满足我们的需求,这就要求我们同时使用两个三个或者个更多的控件一起使用。有些常用的组合我就把他写成一个自定的的组件,这样就方便了以后自己的使用。本人新手,研究一天写出的组件虽然实现了自己想要的功能,但代码应该还有许多不足的地方,希望大家可以指点一下。写本帖的目的是防止自己忘记。

首先第一步,写一个布局文件my_title.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="50dip"
android:id="@+id/my_title_layout"
android:orientation="horizontal" > <Button
android:id="@+id/my_titlt_left_btn"
android:layout_width="60dip"
android:layout_height="fill_parent"
android:background="#00000000"
android:text="返回"
/>
<TextView
android:id="@+id/my_title_middle_text"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textColor="#f00"
android:textSize="20sp"
android:text="我是标题栏"
/>
<Button
android:id="@+id/my_title_right_btn"
android:layout_height="fill_parent"
android:layout_width="60dip"
android:background="#00ffffff"
android:text="删除"
/>
</LinearLayout>

第二步,为了方便封装后设置某些属性,因此我们要为该组件添加一些自定义属性!在values文件夹下新建attrs.xml文件。

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="myTitle">
<attr name="background" format="reference|color"/>
<attr name="middlebackground" format="reference|color"/>
<attr name="btnleftbackground" format="reference|color"/>
<attr name="btnrightbackground" format="reference|color"/>
</declare-styleable>
</resources>

第三步,我们新建一个类并继承于LinearLayout在这个类里面引用上面的布局文件my_title.xml。并在里面定义一些我们要用到的方法,和初始化我们自定义的属性。

 package com.Jett.mytitle;

 import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView; public class MyTitleView extends LinearLayout { private LinearLayout ll;
private Button btnleft,btnright;
private TextView middletext; //构造函数
public MyTitleView(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
public MyTitleView(Context context, AttributeSet attrs) {
super(context, attrs);
//初始化控件属性
LayoutInflater.from(context).inflate(R.layout.my_title,this,true); ll = (LinearLayout)this.findViewById(R.id.my_title_layout);
btnleft = (Button)this.findViewById(R.id.my_titlt_left_btn);
btnright = (Button)this.findViewById(R.id.my_title_right_btn);
middletext = (TextView)this.findViewById(R.id.my_title_middle_text); //初始化
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.myTitle);
//设置标题栏颜色和中间颜色(默认没有颜色)
setBackground(a.getColor(R.styleable.myTitle_background,0));
setMiddleTextBackgroundColoe(a.getColor(R.styleable.myTitle_middlebackground,0));
//设置左右按钮的背景图片(默认没有背景图片)
setBtnLeftBackground(a.getResourceId(R.styleable.myTitle_btnleftbackground,0));
setBtnRightBackground(a.getResourceId(R.styleable.myTitle_btnrightbackground,0)); //同步一下属性
a.recycle();
}
//设置显示的文字
public void setBtnLeftText(String text)
{
btnleft.setText(text);
}
public void setBtnRightText(String text)
{
btnright.setText(text);
}
public void setMiddleText(String text)
{
middletext.setText(text);
}
//设置背景颜色
public void setBackground(int resId)
{
ll.setBackgroundColor(resId);
}
public void setMiddleTextBackgroundColoe(int color)
{
middletext.setBackgroundColor(color);
}
//设置左右按钮的背景图片
public void setBtnLeftBackground(int resId)
{
btnleft.setBackgroundResource(resId);
}
public void setBtnRightBackground(int resId)
{
btnright.setBackgroundResource(resId);
} //设置左右按钮的点击事件
public void setLeftOnClick(OnClickListener clickListener)
{
btnleft.setOnClickListener(clickListener);
}
public void setRightOnClick(OnClickListener clickListener)
{
btnright.setOnClickListener(clickListener);
}
}

写到这我们自定义的组件其实已经写好了。下面就是如何使用了。
首先我们在布局文件里面引用。使用自定义属性需要加入xmlns:mytitle="http://schemas.android.com/apk/res/com.Jett.mytitle" 红色字体随便写,不过下面要用来调用自定义属性。蓝色字体是包名。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mytitle="http://schemas.android.com/apk/res/com.Jett.mytitle"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="0dip"
android:paddingRight="0dip"
android:paddingTop="0dip"
tools:context=".MainActivity" > <com.Jett.mytitle.MyTitleView
android:id="@+id/Mian_mytitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
mytitle:background="#2aa9f4"
mytitle:middlebackground="#2595ef"
mytitle:btnrightbackground="@drawable/mytitle_btn_style"
>
</com.Jett.mytitle.MyTitleView> </RelativeLayout>

还有就是在Activity 里面设置一些组件方法

 package com.Jett.mytitle;

 import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.Color; public class MainActivity extends Activity {
private MyTitleView mytitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mytitle = (MyTitleView)this.findViewById(R.id.Mian_mytitle); mytitle.setMiddleText("标题");
mytitle.setBtnRightText("右侧");
mytitle.setLeftOnClick(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你点击了左侧按钮!", 1).show();
}
});
}
}

这样就好了,对了把按钮样式也贴出来一下吧。
在res文件夹下新建文件夹drawable在给文件夹下新建文件mytitle_btn_style.xml。

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按钮按下 -->
<item android:state_pressed="true">
<shape>
<!-- 填充颜色 -->
<solid
android:color="#1a6d99"/>
<!-- 间隔 -->
<padding android:left="0dp" android:right="0dp"
android:top="0dp" android:bottom="0dp"/> </shape>
</item> <item android:state_enabled="true">
<shape>
<!-- 间隔 -->
<padding android:left="0dp" android:right="0dp"
android:top="0dp" android:bottom="0dp"/>
<!-- 填充 -->
<solid
android:color="#2aa9f4"/><!-- 填充的颜色 -->
</shape>
</item>
</selector>

到此我们自定义的组件就完成了。可以方便以后我们项目中的使用。
效果如下:

  

android 自定义组件的更多相关文章

  1. Android自定义组件之自动换行及宽度自适应View:WordWrapView

    目的: 自定义一个ViewGroup,里面的子view都是TextView,每个子view  TextView的宽度随内容自适应且每行的子View的个数自适应,并可以自动换行 一:效果图 二:代码 整 ...

  2. Android自定义组件系列【7】——进阶实践(4)

    上一篇<Android自定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识,这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpan ...

  3. Android自定义组件系列【6】——进阶实践(3)

    上一篇<Android自定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计划 ...

  4. Android自定义组件系列【5】——进阶实践(2)

    上一篇<Android自定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这一 ...

  5. Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动

    在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...

  6. Android自定义组件

    [参考的原文地址] http://blog.csdn.net/l1028386804/article/details/47101387效果图: 实现方式: 一:自定义一个含有EditText和Butt ...

  7. Android 自定义组件之如何实现自定义组件

    参考链接:http://blog.csdn.net/jjwwmlp456/article/details/41076699 简介 Android提供了用于构建UI的强大的组件模型.两个基类:View和 ...

  8. Android自定义组件系列【3】——自定义ViewGroup实现侧滑

    有关自定义ViewGroup的文章已经很多了,我为什么写这篇文章,对于初学者或者对自定义组件比较生疏的朋友虽然可以拿来主义的用了,但是要一步一步的实现和了解其中的过程和原理才能真真脱离别人的代码,举一 ...

  9. Android 自定义组件,自定义LinearLayout,ListView等样式的组件

    今天讲的其实以前自己用过,就是在网上拿下来的把图片裁剪成圆形的方法,之前的随笔也介绍过的, 用法就是,在布局里写控件或者组件的时候得把从com开始到你写的那个类的所有路径写下来. 至于我们该怎么创建呢 ...

  10. 自己写的几个android自定义组件

    http://www.see-source.com/androidwidget/list.html 多多指点,尤其是自定义组件的适配问题,希望能有更好的方法

随机推荐

  1. C# TextBox中只允许输入数字的方法

    1.在Winform(C#)中要实现限制Textbox只能输入数字,一般的做法就是在按键事件中处理, 判断keychar的值.限制只能输入数字,小数点,Backspace,del这几个键.数字0-9所 ...

  2. .NET简谈构件系统开发模式

    转自[王清培] http://www.cnblogs.com/wangiqngpei557/archive/2011/06/14/2080416.html 在本人的“.NET简谈插件系统开发模式”一文 ...

  3. 黄聪:wordpress后台导致fonts.googleapis.com、ajax.googleapis.com加载慢的解决方法

    方法1.使用我做的插件.[googleapis-to-useso] 方法2.在functions.php文件里面添加下面的代码就行了. if(is_admin()) { function hcsem_ ...

  4. 【Java】PrettyTime

    package test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.D ...

  5. NoSQL分类及ehcache memcache redis 三大缓存的对比

    NoSQL分类 由于NoSQL中没有像传统数据库那样定义数据的组织方式为关系型的,所以只要内部的数据组织采用了非关系型的方式,就可以称之为NoSQL数据库.目前,可以将众多的NoSQL数据库按照内部的 ...

  6. 94、EventBus框架 ---- 转载

    EventBus使用之基础 http://blog.csdn.net/yanbober/article/details/45667363 EventBus框架库代码走读  http://blog.cs ...

  7. checkbox 选中个数

    背景: 1 Choose1 全选checkbox ,选中此checkbox,子列表的checkbox全部为选中状态. 2 在子列表中如果去掉一个checkbox则Choose1 的全选状态也改为不选中 ...

  8. JAVA错误:Cannot refer to a non-final variable * inside an inner class defined in a different method

    在使用Java局部内部类或者内部类时,若该类调用了所在方法的局部变量,则该局部变量必须使用final关键字来修饰,否则将会出现编译错误“Cannot refer to a non-final vari ...

  9. Enterprise Library 5.0 系列教程

    1. Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (初级) 2. Microsoft Enterprise L ...

  10. 生成ldf数据库文件

    .在我的电脑中删除LOG文件 c.附加数据库: 企业管理器--服务器--数据库--右键--附加数据库 此法将生成新的LOG,大小只有500多K