自定义组合控件

如上图所示的四个模块都如出一辙,为了只定义其中一个的格式然后其他均利用这个格式更方面的直接生成,我们引入自定义组合控件,

这里给出的例子中这个自定义组合控件由两个TextView,一个CheckBox和一个View组成(注意每个模块最下面那条横线用view实现,background设置为黑#000000,高度设置为0.2dip)


自定义组合控件的过程:

1. 自定义一个View,这个View一般来说继承相对布局或者线性布局,(即必须继承ViewGroup 的子类,这个ViewGroup的特点是可以把其他View放在其中)。

public class SettingItemView extends RelativeLayout{}

2. 实现(重写)这个父类的构造方法,一般来说需要在构造方法里初始化自定义的的布局文件。本例中是在每个构造方法里都添加iniview()

<span style="font-size:18px;">       private void <span style="color:#ff6666;">iniView</span>(Context context){
<span style="white-space:pre">	</span>//把一个布局文件转化为view加载进来		</span>
<span style="font-size:18px;"><span style="white-space:pre">	</span>View.inflate(context, R.layout.setting_item_view, this);
	cb_status=(CheckBox) this.findViewById(R.id.cb_status);
	tv_title=(TextView) this.findViewById(R.id.tv_title);
	tv_desc=(TextView) this.findViewById(R.id.tv_desc);
		
	}
	
 	public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		<span style="color:#ff6666;">iniView(context)</span>;
	}

	public SettingItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		<span style="color:#ff6666;">iniView(context)</span>;
	}

	public SettingItemView(Context context) {
		super(context);
		<span style="color:#ff6666;">iniView(context)</span>;
	}</span>

3. 根据需求增加一些API方法。

public  boolean isChecked(){
		//校验组合是否被选中,想实现的效果是点击这个组合控件的任何位置都可以选中勾选框
		if(cb_status.isChecked()){
			return true;
		}else{
			return false;
		}
	}
	//将组合控件设置为勾选的状态,同样设置随之改变的描述信息
	public void setChecked(boolean checked){
		if(checked){
			setDesc(desc_on);
		}else{
			setDesc(desc_off);
		}
		cb_status.setChecked(checked);
	}
	private void setDesc(String text) {  tv_desc.setText(text); }

4. 为了更加方便,可以根据需求自定义一些属性,可以参照TextView 的属性创建。

5. 自定义命名空间,例如

xmlns:android=http://schemas.android.com/apk/res/android

xmlns:android=http://schemas.android.com/apk/res/<包名>

6.自定义我们的属性,在工程里res/values目录下创建attrs.xml文件如下

<span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <declare-styleable name="TextView">
        <attr name="title" format="string"/>
        <attr name="desc_on" format="string"/>
        <attr name="desc_off" format="string"/>
    </declare-styleable>
    
</resources></span></span>

7.使用我们自定义的属性

例如:

<span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:example="http://schemas.android.com/apk/res/com.example.mobilesafe"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="55dip"
        android:background="#8866ff00"
        android:text="设置中心"
        android:textSize="22sp"
        android:textColor="#000000"
        android:gravity="center"        
        />
    <com.example.mobilesafe.ui.SettingItemView
        android:id="@+id/siv_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        example:desc_on="自动更新已经开启"
        example:desc_off="自动更新已经关闭"
        example:title="设置自动更新"
        >
    </com.example.mobilesafe.ui.SettingItemView>
    

</LinearLayout>
</span></span>


8.在我们自定义控件的带有两个参数的构造方法里AttributeSet attrs 取出我们的属性值,关联自定义文件对应的控件。

<span style="font-size:18px;">	public SettingItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		iniView(context);
		String title=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.mobilesafe","title");
		desc_on=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.mobilesafe", "desc_on");
		desc_off=attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.mobilesafe", "desc_off");
		tv_title.setText(title);
		setDesc(desc_off);
	}</span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值