android 自定义组件
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 自定义组件的更多相关文章
- Android自定义组件之自动换行及宽度自适应View:WordWrapView
目的: 自定义一个ViewGroup,里面的子view都是TextView,每个子view TextView的宽度随内容自适应且每行的子View的个数自适应,并可以自动换行 一:效果图 二:代码 整 ...
- Android自定义组件系列【7】——进阶实践(4)
上一篇<Android自定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识,这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpan ...
- Android自定义组件系列【6】——进阶实践(3)
上一篇<Android自定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计划 ...
- Android自定义组件系列【5】——进阶实践(2)
上一篇<Android自定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这一 ...
- Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动
在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...
- Android自定义组件
[参考的原文地址] http://blog.csdn.net/l1028386804/article/details/47101387效果图: 实现方式: 一:自定义一个含有EditText和Butt ...
- Android 自定义组件之如何实现自定义组件
参考链接:http://blog.csdn.net/jjwwmlp456/article/details/41076699 简介 Android提供了用于构建UI的强大的组件模型.两个基类:View和 ...
- Android自定义组件系列【3】——自定义ViewGroup实现侧滑
有关自定义ViewGroup的文章已经很多了,我为什么写这篇文章,对于初学者或者对自定义组件比较生疏的朋友虽然可以拿来主义的用了,但是要一步一步的实现和了解其中的过程和原理才能真真脱离别人的代码,举一 ...
- Android 自定义组件,自定义LinearLayout,ListView等样式的组件
今天讲的其实以前自己用过,就是在网上拿下来的把图片裁剪成圆形的方法,之前的随笔也介绍过的, 用法就是,在布局里写控件或者组件的时候得把从com开始到你写的那个类的所有路径写下来. 至于我们该怎么创建呢 ...
- 自己写的几个android自定义组件
http://www.see-source.com/androidwidget/list.html 多多指点,尤其是自定义组件的适配问题,希望能有更好的方法
随机推荐
- 互联网 DBA 需要做那些事(转)
众所周知,互联网DBA与传统行业DBA有很大的不同,那就是管理的机器多,新技术更新快,面对的开发多.网络环境复杂.要求7*24待机:这样就 导致互联网DBA的工作在传统DBA工作之上,增加了更多的复杂 ...
- PHP 正则表达式常用函数使用小结
在PHP中有两套正则表达式函数库.一套是由PCRE(Perl Compatible Regular Expression)库提供的.PCRE库使用和Perl相同的语法规则实现了正则表达式的模式匹配,其 ...
- jQuery插件:用于获取元素自身的HTML内容
jQuery.fn.outerHTML = function(s) { return (s) ? this.before(s).remove() : $("<Hill_man>& ...
- Form_Form Builder开发基于视图页面和自动代码生成包(案例)
2014-01-06 Created By BaoXinjian
- 转-Activity之间数据传递之Intent数据传递
Intent意图 可用于Activity之间的数据传递,一般可分为下面两种情况,从当前Activity传递到目标Activity后有无返回值: 1.传递后无返回值的情况: 1 2 3 4 5 6 7 ...
- Input gameobject vector3 c#
Input类中的常用方法 bool w=Input.GetKey(KeyCode.W);//检测是否按下键盘W Input.GetKeyDown(KeyCode.W);//表示检测按下时 Input. ...
- 内联函数inline
1:使用inline函数的时候,必须使函数体和inline说明结合一起,否则编译器将视他为普通函数处理: false: inline void Coord::setcoord(int a,int b) ...
- Linux vmstat命令--监控CPU 性能分析
top是给Linux设计的.在FreeBSD VM里面的Free概念和其他OS完全不同,使用top查看Free内存对于FreeBSD来说可以说没什么意义.正确的方法是看vmstat. vmstat是V ...
- 词典对象 NSDictionary与NSMutableDictionary
做过Java语言或者 C语言开发的朋友应该很清楚关键字map 吧,它可以将数据以键值对儿的形式储存起来,取值的时候通过KEY就可以直接拿到对应的值,非常方便,是一种非常常用的数据结构.在Objecti ...
- 离线使用echarts及一些细节
最近要做图表,用js起来太麻烦,所以就找些开源的库来用,发现echarts挺不错, echarts的文档把所有东西都说的很明白了,直接下载zip包,要是想离线使用的话只需要引用下载包里面的dist文件 ...