简单的自定义组合控件
2016-07-12 15:25
281 查看
最近在学习自定义组合控件,看到了一篇文章,照这样子写了一段代码,并做了一些修改。
首先以xml文件为组合控件的基础代码如下:
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_title" android:padding="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是标题一"/> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_title" android:paddingLeft="5dp" android:text="没有被选中"/> <CheckBox android:id="@+id/box_status" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignBottom="@+id/tv_content" android:layout_marginRight="5dp" /> </RelativeLayout>
自定义组件当然可以定义属性了,下面为本次的控件做属性定义:
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomRelative"> <attr name="contenttitle" format="string"/> <attr name="contenton" format="string"/> <attr name="contentoff" format="string"/> </declare-styleable> </resources>
以上是为本次组件定义的属性,接下来就在控件的构造方法中获取到属性值以便使用。
import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RelativeLayout; import android.widget.TextView; /** * Created by UWorks on 2016/7/12. */ public class CustomRelative extends RelativeLayout { private TextView title; private TextView content; private CheckBox box; private String mTitle; private String mContenton; private String mContentoff; public CustomRelative(Context context, AttributeSet attrs) { super(context, attrs); View view = View.inflate(context,R.layout.layout_custom, this); title = (TextView) view.findViewById(R.id.tv_title); content = (TextView) view.findViewById(R.id.tv_content); box = (CheckBox) view.findViewById(R.id.box_status); TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.CustomRelative); int n = array.getIndexCount(); for (int i = 0;i<n;i++){ int attr = array.getIndex(i); switch (attr){ case R.styleable.CustomRelative_contenttitle: mTitle = array.getString(attr); Log.d("组合控件",mTitle); break; case R.styleable.CustomRelative_contenton: mContenton = array.getString(attr); Log.d("组合控件",mContenton); break; case R.styleable.CustomRelative_contentoff: mContentoff = array.getString(attr); Log.d("组合控件",mContentoff); break; } } if (mTitle != null){ title.setText(mTitle); } if (mContentoff != null){ content.setText(mContentoff); } array.recycle(); box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked){ content.setText(mContenton); }else { content.setText(mContentoff); } } }); } }
这里需要注意的是<pre name="code" class="java">View.inflate(context,R.layout.layout_custom, this);这句话表明在其他布局使用本次定义的控件时,以布局为跟布局,否则将不会显示自定义的组合控件
相关文章推荐
- oracle 复制表操作
- Linux下安装、配置、启动Apache
- python sqlserver 变量形式插入数据
- 并发框架Disruptor译文
- 信号相关函数的介绍及使用
- 260. Single Number III
- 不忘历史、维护中国海权
- (浅谈)Jpush推送通知点击跳转到具体界面
- NYOJ-括号配对问题
- 工作问题总结
- UVA1616 商队抢劫者
- phpmyadmin增加外键
- 【Codeforces 609E】【Stree】【最小生成树】
- nginx 的日志记录 response 的报文头
- 桥接模式
- 内存分配函数malloc calloc realloc
- 安装 Kali Linux 后需要做的 20 件事
- Android四大基本组件介绍与生命周期
- java之时间格式
- Chapter2~快速入门