1.自定义控件--自定义属性attr

1 属性文件 values-attrs
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="SettingItemView">
        <attr name="des" format="string|reference"></attr>
        <attr name="itemBg" format="string">
            <enum name="first" value="0"></enum>
            <enum name="middle" value="1"></enum>
            <enum name="last" value="2"></enum>
        </attr>
        <attr name="checked" format="boolean"></attr>
        <attr name="isVisiable" format="boolean"></attr>
    </declare-styleable>
    
    <declare-styleable name="CircleImageView">
        <attr name="civ_border_width" format="dimension" />
        <attr name="civ_border_color" format="color" />
        <attr name="civ_border_overlay" format="boolean" />
        <attr name="civ_fill_color" format="color" />
    </declare-styleable>

</resources>

2 自定义控件 

package org.heima.mobilesafe01.view;

import org.heima.mobilesafe01.R;
import org.heima.mobilesafe01.utils.L;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SettingItemView extends LinearLayout {

	private ImageView mIv;
	private boolean mChecked;
	private boolean mIsVisable;

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

	public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		L.d("SettingItemView:3");
	}

	public SettingItemView(Context context, AttributeSet attrs) {// 布局文件
		super(context, attrs);
		/*
		 * heima:itemBg="first" heima:checked="true" heima:isVisiable="true"
		 */
		// 事多
		View view = View.inflate(context, R.layout.view_setting_item, this);// view_setting_item--View
																			// ---
//		L.d(view.toString());																	// parent
		// 怎么样拿到布局文件中的数据
		TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.SettingItemView);
		String des = ta.getString(R.styleable.SettingItemView_des);
		int itemBg = ta.getInt(R.styleable.SettingItemView_itemBg, -1);// 获取枚举值
		mChecked = ta.getBoolean(R.styleable.SettingItemView_checked, false);
		mIsVisable = ta.getBoolean(R.styleable.SettingItemView_isVisiable,
				false);

		TextView tv = (TextView) view.findViewById(R.id.tv_setting_item_des);
		tv.setText(des);

		switch (itemBg) {
		case 0:
			setBackgroundResource(R.drawable.iv_first_selector);
			break;
		case 1:
			setBackgroundResource(R.drawable.iv_middle_selector);
			break;
		case 2:
			setBackgroundResource(R.drawable.iv_last_selector);
			break;
		default:
			setBackgroundResource(R.drawable.iv_first_selector);
			break;
		}

		mIv = (ImageView) view.findViewById(R.id.iv_setting_item_checked);

		setChecked();
		setVisable();
		
		// 让这个可点击
		setClickable(true);
		// 回收一下
		ta.recycle();
	}

	private void setVisable() {
		mIv.setVisibility(mIsVisable?View.VISIBLE:View.INVISIBLE);
	}

	private void setChecked() {
		// if(mChecked){
		// mIv.setImageResource(R.drawable.on);
		// }else {
		// mIv.setImageResource(R.drawable.off);
		// }
		mIv.setImageResource(mChecked ? R.drawable.on : R.drawable.off);
	}
	
	// 提供外面设置,是否被选择
	public void setChecked(boolean isChecked){
		this.mChecked=isChecked;
		// 更新UI
		 setChecked();
	}
	
	public boolean isChecked(){
		return this.mChecked;
	}
	
	
	public void toggle(){
		this.mChecked=!this.mChecked;
		// 更新UI
		setChecked();
	}
	
	

}


3 使用自定义控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:heima="http://schemas.android.com/apk/res/org.heima.mobilesafe01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    
     >

    <TextView
        style="@style/TvTitleStyle"
        android:text="设置中心" 
        />

    <!--
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="@drawable/iv_first_selector"
        android:padding="10dp" 
        android:clickable="true"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingLeft="10dp"
            android:text="版本更新" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingRight="10dp"
            android:src="@drawable/on" />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/iv_middle_selector"
        android:padding="10dp" 
        android:clickable="true"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingLeft="10dp"
            android:text="版本更新" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingRight="10dp"
            android:src="@drawable/on" />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/iv_last_selector"
        android:padding="10dp" 
        android:clickable="true"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingLeft="10dp"
            android:text="版本更新" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingRight="10dp"
            android:src="@drawable/on" />
    </RelativeLayout>
    -->

    <org.heima.mobilesafe01.view.SettingItemView
        android:id="@+id/siv_version"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_marginTop="20dp"
        heima:des="版本更新"
     	heima:itemBg="first"
     	heima:checked="true"
     	heima:isVisiable="true"
        />
    <org.heima.mobilesafe01.view.SettingItemView
         android:id="@+id/siv_call_sms_safe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        heima:des="拦截骚扰设置"
     	heima:itemBg="last"
     	heima:checked="true"
     	heima:isVisiable="true"
        />
    
    <org.heima.mobilesafe01.view.SettingItemView
        android:id="@+id/siv_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_marginTop="10dp"
        heima:des="归属地显示设置"
     	heima:itemBg="first"
     	heima:checked="false"
     	heima:isVisiable="true"
        />
    <org.heima.mobilesafe01.view.SettingItemView
        android:id="@+id/siv_style_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        heima:des="归属地样式显示设置"
     	heima:itemBg="last"
     	heima:checked="true"
     	heima:isVisiable="false"
        />

    <!-- <org.heima.mobilesafe01.view.CircleImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/mvdd"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        /> -->

</LinearLayout>




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值