自定义控件

1、自定义控件布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bt_dialog"
    android:clickable="true"
     >
    
    <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="标题"
            android:textSize="18sp"
            android:id="@+id/tv_setting_title"
            android:layout_marginBottom="5dip"/>
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="简介简介"
            android:id="@+id/tv_setting_con"
            android:layout_below="@id/tv_setting_title"/>
        
        <CheckBox android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/cb_setting_chose"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:clickable="false"/>
        
        <TextView android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="@android:color/darker_gray"
            android:layout_below="@id/tv_setting_con"/>
    

</RelativeLayout>

 

2、在res/values文件夹下新建 attrs.xml 用来定义 自定义控件属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 设置控件名,当自定义控件很多时,用来 识别 是哪一个  -->
    <declare-styleable name="SettingCenterItemView">

        <!--
             Defines whether a child is limited to draw inside of its bounds or not.
             This is useful with animations that scale the size of the children to more
             than 100% for instance. In such a case, this property should be set to false
             to allow the children to draw outside of their bounds. The default value of
             this property is true.
        -->
        <!-- 属性名为 title   类型为 string -->
        <attr name="title" format="string" />
        <!-- 属性名为 content   类型为 string -->
        <attr name="content" format="string" />
    </declare-styleable>
</resources>

 

3、在布局中调用 自定义控件

  添加自定义的命名空间  xmlns:自定义空间名="http://schemas.android.com/apk/res/工程包名"

  通过     自定义空间名:属性名 = ""   的方式调用

  以下为例说明:当执行到 myroid:title="自动更新设置" 时,会在 xmlns: 处找到名为 myroid 的工程 com.example.shoujiweishi , 然后在 com.example.shoujiweishi 工程的 attrs.xml 文件中 找到对应的属性名 title

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myroid="http://schemas.android.com/apk/res/com.example.shoujiweishi"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView style="@style/activity_title"
        android:text="设置中心"/>
    
    <include layout="@layout/item_setting_list"/>
    
    <com.example.shoujiweishi.view.SettingListView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        myroid:title="自动更新设置"
        myroid:content="自动更新已经关闭-自动更新已经打开"></com.example.shoujiweishi.view.SettingListView>
    
    <com.example.shoujiweishi.view.SettingListView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        myroid:title="黑名单拦截设置"
        myroid:content="黑名单拦截设置已经关闭-黑名单拦截设置已经打开"></com.example.shoujiweishi.view.SettingListView>

</LinearLayout>

 

4、新建一个类,继承一个 控件 类,并设置内容,通过设置的空间名和属性名获得布局中设置的参数并设置给自定义控件

package com.example.shoujiweishi.view;

import com.example.shoujiweishi.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SettingListView extends RelativeLayout {

    private TextView tv_title;
    private TextView tv_con;
    private CheckBox cb_chose;
    private View view;

    /**
     * 代码实例化 调用该方法
     * @param context
     */
    public SettingListView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        
        initView();
    }

    

    /**
     * 配置文件中,反射实例化 设置属性参数 
     * @param context
     * @param attrs
     */
    public SettingListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        
        initView();
        initEvent();
        
        //通过 空间名 和 属性名获得 设置的属性参数
        String content = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.shoujiweishi", "content");
        String title = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.shoujiweishi", "title");
        
        tv_title.setText(title);
        tv_con.setText(content);
    }
    
    private void initEvent() {
        // TODO Auto-generated method stub
        view.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                
            }
        });
    }



    private void initView(){
        view = View.inflate(getContext(), R.layout.item_setting_list, null);
        tv_title = (TextView) view.findViewById(R.id.tv_setting_title);
        tv_con = (TextView) view.findViewById(R.id.tv_setting_con);
        cb_chose = (CheckBox) view.findViewById(R.id.cb_setting_chose);
        
        
        
        addView(view);
    }

    

}

 

posted @ 2016-03-15 17:06  znyyjk  阅读(141)  评论(0编辑  收藏  举报