自定义控件之功能开关

1、效果图

国庆假期不敢出去看人头,在家把笔记整理了一下更新到博客,如下:



步骤:

(1)自定View(已经存在的控件布局)

(2)自定义容器类的子类 来添加上面的view

(3)自定义属性

1) 自定义命名空间
            xmlns:前缀="http://schemas.android.com/apk/res/包名"

2)创建attrs.xml文件(可以参考系统属性的写法)

3)xml布局文件中使用属性

4)代码中解析属性

2、布局文件

主布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:brother="http://schemas.android.com/apk/res/com.example"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
 >
    <com.example.Item 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        brother:desc="模式一"
        brother:bgselector="first"
        />
    <com.example.Item 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        brother:desc="模式二"
        brother:bgselector="middle"
        />
    <com.example.Item 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        brother:desc="模式三"
        brother:bgselector="last"
        />

</LinearLayout>


自定义控件布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rv_item_mode"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dip"
    android:layout_marginRight="15dip"
    android:paddingBottom="5dip"
    android:paddingTop="5dip"
     >
     
    <TextView 
        android:id="@+id/tv_item_mode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textSize="18dp"
        android:text="模式一"
        />
    <ImageView 
        android:id="@+id/iv_item_toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/off"
        />

</RelativeLayout>


3、Activity

public class MainActivity extends Activity {



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		initView();
	}

	/**
	 * 初始化界面
	 */
	private void initView() {
		setContentView(R.layout.activity_main);
		
	}

}

4、自定义容器类的子类

public class Item extends RelativeLayout {

	private View rootView;
	private TextView tv_desc;
	private ImageView iv_toggle;
	private boolean isOpen;

	/**
	 * 布局文件实例化使用
	 * 
	 * @param context
	 * @param attrs
	 */
	public Item(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
		initData(attrs);
		initEvent();
	}

	/**
	 * 初始化开关点击事件
	 */
	private void initEvent() {
		// 给rootView添加点击事件
		rootView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 切换开关的状态

				if (isOpen) {
					// 设置iv为打开的图片
					iv_toggle.setImageResource(R.drawable.on);
					isOpen=false;
				} else {
					iv_toggle.setImageResource(R.drawable.off);
					isOpen=true;
				}

			}
		});
	}

	/**
	 * 初始化数据
	 * 
	 * @param attrs
	 */
	private void initData(AttributeSet attrs) {
		// 取出属性
		String desc = attrs.getAttributeValue(
				"http://schemas.android.com/apk/res/com.example", "desc");
		// 取背景选择器
		String bgtype = attrs.getAttributeValue(
				"http://schemas.android.com/apk/res/com.example", "bgselector");

		boolean isDisableToggle = attrs.getAttributeBooleanValue(
				"http://schemas.android.com/apk/res/com.example",
				"isdisabletoggle", false);

		if (isDisableToggle) {
			// 隐藏
			iv_toggle.setVisibility(View.GONE);
		}
		// 设置属性
		tv_desc.setText(desc);

		// 根据bgtype设置背景选择器
		switch (Integer.parseInt(bgtype)) {
		case 0:
			rootView.setBackgroundResource(R.drawable.iv_first_selector);
			break;
		case 1:
			rootView.setBackgroundResource(R.drawable.iv_middle_selector);

			break;
		case 2:
			rootView.setBackgroundResource(R.drawable.iv_last_selector);

			break;

		default:
			break;
		}

	}

	/**
	 * 初始化控件
	 */
	private void initView() {
		// 自定义控件
		// view已经添加到Relativelayout中
		rootView = View.inflate(getContext(), R.layout.item_view, this);
		tv_desc = (TextView) rootView.findViewById(R.id.tv_item_mode);
		iv_toggle = (ImageView) rootView.findViewById(R.id.iv_item_toggle);
	}

	/**
	 * 代码实例化使用
	 * 
	 * @param context
	 */
	public Item(Context context) {
		this(context, null);
	}

}

5、自定义属性

这里为求美观,用了背景选择器
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="Item">

        <!--
             Should the layout be a column or a row?  Use "horizontal"
             for a row, "vertical" for a column.  The default is
             horizontal.


        -->
        <attr name="desc" format="string" />
        <!-- 设置背景选择器 -->
        <attr name="bgselector">
            <enum name="first" value="0" />
            <enum name="middle" value="1" />
            <enum name="last" value="2" />
        </attr>
        <attr name="isdisabletoggle" format="boolean"></attr>
    </declare-styleable>

</resources>

打完收工~有兴趣的朋友可以在里面添加按钮状态的回调,作为控制功能的开关。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值