在android中有时我们需要根据需求自定义控件,自定义组合控件是较简单的一种,下面我们来实现一下设置里面的常用效果:
首先,自定义组合空间主要分为以下几个步骤:
1,编写自定义控件的布局文件.
2,在values目录下,添加自定义控件相对应的,属性资源文件attrs.xml.用来自定义一些属性.
3,创建自定义控件的View对象继承ViewGroup的一个子类就可以了(一般选择RelativeLayout或者LinearLayout),重写参数列表为(Context context, AttributeSet attrs)的构造方法.
4,在构造方法中通过context上下文的obtainStyledAttributes方法找到自定义的属性数组TypedArray(获取结束后记得调用TypedArray.recycle()方法,释放资源.)
5,通过引用自定义组合控件的完整类名就可以在其他布局文件中使用自定义组合控件了,
6,添加自定义组合控件的命名空间声明,就可以在xml布局文件中直接设置第2步中自定义的属性了.
下面一步一步来试试看:
0,创建一个android工程.
1,编写自定义控件的布局文件.
ui_setting.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="60dp" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:text="设置的说明" android:textSize="26sp" /> <TextView android:id="@+id/tv_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_title" android:layout_marginLeft="10dp" android:text="状态" android:textColor="@android:color/darker_gray" android:textSize="14sp" /> </RelativeLayout> <CheckBox android:id="@+id/cb_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:clickable="false" android:focusable="false" /> </RelativeLayout>
2.在values目录下,添加自定义控件相对应的属性资源文件attrs.xml.用来自定义一些属性.
attrs.xml:
<resources> <declare-styleable name="MyView"> <attr name="title" format="string" /> <attr name="desc_on" format="string" /> <attr name="desc_off" format="string" /> </declare-styleable> </resources>
3,创建自定义控件的View对象继承ViewGroup的一个子类就可以了(一般选择RelativeLayout或者LinearLayout),重写参数列表为(Context context, AttributeSet attrs)的构造方法.
4,在构造方法中通过context上下文的obtainStyledAttributes方法找到自定义的属性数组TypedArray(获取结束后记得调用TypedArray.recycle()方法,释放资源.)
public class MyView extends LinearLayout { private CheckBox cb_status; private TextView tv_desc; private TextView tv_title; private String title; private String desc_on; private String desc_off; private void init(Context context) { //为当前view对象设置布局 View.inflate(context, R.layout.ui_setting , this); cb_status = (CheckBox) findViewById(R.id.cb_status); tv_desc = (TextView) findViewById(R.id.tv_desc); tv_title = (TextView) findViewById(R.id.tv_title); } //复写父类的构造方法 public MyView(Context context, AttributeSet attrs) { super(context, attrs); //初始化view对象 init(context); // 通过上下文context获取自定义属性 TypedArray typeArr = context.obtainStyledAttributes(attrs,R.styleable.MyView); // 获取在xml文件中,自定义属性的值 title = typeArr.getString(R.styleable.MyView_title); desc_on = typeArr.getString(R.styleable.MyView_desc_on); desc_off = typeArr.getString(R.styleable.MyView_desc_off); //释放资源 typeArr.recycle(); // 设置title setTitle(title); // 设置选中状态 if(isChecked()){ setDesc(desc_on); }else{ setDesc(desc_off); } // 添加点击事件 this.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setChecked(!isChecked()); } }); } public boolean isChecked() { return cb_status.isChecked(); } public void setChecked(boolean checked){ this.cb_status.setChecked(checked); if(checked){ setDesc(desc_on); }else{ setDesc(desc_off); } } public void setTitle(String title){ tv_title.setText(title); } public void setDesc(String desc){ this.tv_desc.setText(desc); } }
5,通过引用自定义组合控件的完整类名就可以在其他布局文件中使用自定义组合控件,
6,添加自定义组合控件的命名空间声明,就可以在xml布局文件中直接设置第2步中自定义的属性.
自定义控件的命名空间一般这样定义:
xmlns : "别名" = "http://schemas.android.com/apk/res/" + "应用程序包名"
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myui="http://schemas.android.com/apk/res/com.***.myview" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 引用自定义控件的完整类名 --> <com.***.myview.MyView android:id="@+id/mview" android:layout_width="wrap_content" android:layout_height="wrap_content" myui:desc_off="关闭" myui:desc_on="开启" myui:title="自定义的组合控件01" > </com.***.myview.MyView> </LinearLayout>
2014年1月7日 17:50:13 笔记---end
相关推荐
手机安全卫士--Android自定义组合控件实现设置功能,界面采用Android自定义组合控件的方式实现,更多详细信息请访问 http://blog.csdn.net/qq_20889581?viewmode=contents 文明的小流氓的博客
自定义组合控件实现title栏.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
一个自定义组合控件,可自定义属性,详情见我的博文: http://blog.csdn.net/ytmfdw/article/details/51459945 另外,例子中,用到了Butterknife,详情配制,见 ...
通过自定义 组合控件 实现带斜分的表头 对理解android 自定义控件 有帮助 eclipse直接import 既可, 如果有错, 检查文件字符的编码格式 源代码编译可运行
jqGrid实现如何自定义组合控件,实现更好用户体验
这个Demo主要实现如何将Android下几个原生的控件封装到一起,产生一个新的控件,称之为“android自定义组合控件”,这种控件可以简化代码,大家有空下载看看吧,谢谢!
今天重点介绍一下如何通过自定义组合控件来提高布局的复用,降低开发成本,以及维护成本。 使用自定义组合控件的好处? 我们在项目开发中经常会遇见很多相似或者相同的布局,比如APP的标题栏,我们从三种方式实现...
Android 组合控件,用自定义方式实现,非简单重写adapter。同时包含了点击事件的响应。
自定义控件属性使用及属性详情;组合实现组合控件,奠定完全绘制自定控件前提。
将attrs.xml文件直接复制到res/values目录下,布局文件放在res/layout目录下
有很多情况下,我们只要运用好Android给我提供好的控件,经过布局巧妙的结合在...下面看一张图,就一个简单的布局,我们就此图来实现一个简单的自定义组合控件。 从上面的图来分析,我们可以看到,这个布局里面是没
黑马程序员Android版金山卫士学习第一天源代码,可以参考代码学习一些小功能的实现。知识点:1、项目的代码组织结构 2、PackageManager 获取应用程序的版本号;...14、自定义组合控件 实现关闭自动更新;
实现条形图和面形图的上下组合与左右组合的切换; 实现饼形图的动态旋转; 在饼形图中各扇区的外弧中心点上显示标识文字; 图表与DataSet对象的连接,并显示数据表中的数据; 图表内容跟随控件的大小而改变;
先看看创建组合控件的好处吧,创建组合控件能够很好的创建具有组合功能的控件集合。那我们一般又是怎么做的了,一般我们来继承一个合适的ViewGroup,再为他创建一个新功能,从而就形成了一个新功能的控件。我们还...
主要介绍了Android组合式自定义控件实现购物车加减商品操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本资源在VS2015环境下通过自定义窗体库组合VB.NET已有控件实现DataGridView控件分页显示功能。
http://blog.csdn.net/amazing_alex/article/details/44872921