安卓自定义组合控件的实现

步骤:

1.先建立一个xml文件做好里面的布局;

2.建立一个对应的类;

3.在需要用到此布局的地方进行调用这个布局;

具体的实现代码:

1.建立的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
    	android:layout_height="63dip"
        >
        <TextView 
            android:textColor="#000000"
            android:layout_marginTop="10dip"
            android:layout_marginLeft="10dip"
            android:id="@+id/tv_title"
            android:textSize="16sp"
            android:text="设置是否自动更新"
            android:layout_width="wrap_content"
    		android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/tv_descri"
            android:layout_marginLeft="10dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_title"
            android:text="自动更新已经关闭"
            android:textColor="#88000000" />
        <CheckBox 
            android:layout_marginRight="10dip"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        
        <View
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip" 
            android:layout_alignParentBottom="true"
            android:background="#000000"
            android:layout_width="fill_parent"
            android:layout_height="0.2dip"
            />

</RelativeLayout>
2.建立一个对应的类:

package com.jit.mobilesafe.ui;

import com.jit.mobilesafe.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
//这里面包括两个textview,一个checkbox,一个view
public class SettingItemView extends RelativeLayout {

	private void initView(Context context) {
		// TODO Auto-generated method stub
		View.inflate(context, R.layout.setting_item_view, this);
	}
	
	public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context);
	}

	public SettingItemView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public SettingItemView(Context context) {
		super(context);
		initView(context);
	}

}
3.在需要此布局文件中进行调用此布局(也就是类)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/tv_home"
        android:background="#8866ff"
        android:textSize="22sp"
        android:textColor="#000000"
        android:text="设置中心"
        android:gravity="center"
        android:layout_width="fill_parent"
    	android:layout_height="55dip"
        />
   
    <span style="color:#ff0000;"><com.jit.mobilesafe.ui.SettingItemView
        android:id="@+id/siv_setting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        
    </com.jit.mobilesafe.ui.SettingItemView>
</span>
</LinearLayout>


总结:

1.自定义一个View 一般来说,继承相对布局,或者是线性布局 ViewGroup

2.实现父类的构造方法,一般来说 需要在构造方法里初始化自定义的布局文件;

3.根据需要,定义一些API方法(也就是我们需要的一些用到的方法);

-------------------------------------------------------------------------------------------------------------

4.根据需要,自定义控件的属性,可以参考TextView属性;

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

          xmlns:<自己起的名字>="http://schemas.android.com/apk/res/<包名>"

          xmlns:itjit="http://schemas.android.com/apk/res/com.jit.mobilesafe"

6.自定义我们的属性,在res/values/attrs.xml 文件中

   例如:

              

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextView">
        <attr name="title" format="string" />
        <attr name="descri_on" format="string" />
        <attr name="descri_off" format="string" />
    </declare-styleable>
</resources>
7.使用我们自定义的属性:

    例如:  

<com.jit.mobilesafe.ui.SettingItemView
        itjit:title="设置自动更新"
        itjit:descri_on="自动更新已经开启"
        itjit:descri_off="自动更新已经关闭"
        android:id="@+id/siv_setting_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
8.在我们自定义控件中带有两个参数的构造方法里利用Attribute 变量取出我们需要的属性值,再关联自定义布局文件对应的控件













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值