自定义控件之功能开关

来源:互联网 发布:福建省软件行业协会 编辑:程序博客网 时间:2024/06/10 03:27

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 {@Overrideprotected 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() {@Overridepublic 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>

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



0 0