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. 【WP之一】]独立存储

    介绍: 提供一个磁盘存储空间,他是一种虚拟的文件系统,能存储小量的数据:在默认的情况下,它只能存储1MB的文件.根据使用方式及功能的不同,独立存储空间又包含两部分:独立设置存储和独立文件存储.除非卸载 ...

  2. 黄聪:Xmind修改默认字体风格设置

    Xmind是一款非常好用的思维导图软件,但默认字体使用宋体不够好看,软件本身不支持设置默认字体,但通过修改配置文件达到配置默认字体的目的 默认控制风格的配置文件位置 XMind\plugins\org ...

  3. MongoDB 3.0 导入命令

    在MongoDB的bin目录下执行 ./mongoimport -h 192.168.77.129 --db test --collection restaurants --drop --file / ...

  4. HUST 1010 The Minimum Length(KMP,最短循环节点,即i-Next[i])

    题意: 有一个字符串A,假设A是“abcdefg”,  由A可以重复组成无线长度的AAAAAAA,即“abcdefgabcdefgabcdefg.....”. 从其中截取一段“abcdefgabcde ...

  5. VMware和CentOS7安装和配置

    准备工作: 下载: 1.VMware-workstation-full-10.0.0-1295980 2.CentOS-7-x86_64-DVD-1511.iso 安装: 1.VMware-works ...

  6. 监控页面所有 ajax请求

    监控所有ajax请求: 你是不是有遇到这样的问题:页面发起两个ajax请求,希望它们都成功以后,再做一个动作? 很容易想到的解决方案是,等其中一个结束以后,再发起另外一个,这个过程用回调函数来完成.  ...

  7. HTML5与HTML4的区别

    一.推出的理由及目标 web浏览器之间的兼容性很低 文档结构不够明确 web应用程序的功能受到了限制 二.语法的改变 内容类型 文件扩展名html htm  内容类型 texthtml   二者不变 ...

  8. 张恭庆编《泛函分析讲义》第二章第2节 $Riesz$ 定理及其应用习题解答

    在本节中, $\scrH$ 均指 $Hilbert$ 空间. 1.在极大闭子空间的交的最佳逼近元 设 $f_1,f_2,\cdots,f_n$ 是 $\scrH$ 上的一组线性有界泛函, $$\bex ...

  9. 查询oracle中所有用户信息

    1.查看所有用户:select * from dba_users;   select * from all_users;   select * from user_users; 2.查看用户或角色系统 ...

  10. android实现 服务器功能

    package com.weijia.tests; import java.io.IOException; import java.net.InetSocketAddress; import java ...