zoukankan      html  css  js  c++  java
  • 024_01自定义控件

    1,首先在xml中做好样式:setting_item.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="60dp" >
     5         <TextView 
     6             android:id="@+id/tv_setting_title"
     7             android:layout_width="wrap_content"
     8             android:layout_height="wrap_content"      
     9             android:layout_margin="5dp"            
    10             android:textSize="20sp"
    11             />
    12         <TextView 
    13             android:id="@+id/tv_setting_description"
    14             android:layout_width="wrap_content"
    15             android:layout_height="wrap_content"
    16             android:textSize="16sp"
    17             android:layout_marginLeft="5dp"
    18             android:layout_below="@id/tv_setting_title"
    19             />
    20         <CheckBox 
    21              android:id="@+id/cb_settingitem"
    22              android:layout_width="wrap_content"
    23              android:layout_height="wrap_content"
    24              android:layout_alignParentRight="true"
    25              android:layout_centerVertical="true"
    26              android:clickable="false"
    27              android:focusable="false"
    28             />
    29        <View
    30            android:layout_width="fill_parent"
    31            android:layout_height="0.5dp"
    32            android:background="#000000"
    33            android:layout_alignParentBottom="true"
    34            />
    35            
    36 </RelativeLayout>

    2,新建一个继承自RelativeLayout的类SettingItem,该类就是自定义的控件。

      在构造函数中首先首先使用View的inflate()方法将setting_item.xml布局文件填充到SettingItem中,然后

    使用findViewById()就可以找到setting_item.xml中的控件  View v = View.inflate(getContext(), R.layout.setting_item, this);

     在完成第3步自定义控件属性后,然后可在该类中获得如下代码中黄色标注的属性value

     1 package com.cskaoyan.mobilemanager.ui;
     2 
     3 import com.cskaoyan.mobilemanager.R;
     4 import android.content.Context;
     5 import android.util.AttributeSet;
     6 import android.view.View;
     7 import android.widget.CheckBox;
     8 import android.widget.RelativeLayout;
     9 import android.widget.TextView;
    10 
    11 public class SettingItem extends RelativeLayout {
    12 
    13     private TextView tv_setting_title;
    14     private TextView tv_setting_description;
    15     private String desc_checkbox_on;
    16     private String desc_checkbox_off;
    17     private CheckBox cb_settingitem;
    18 
    19 
    20     public SettingItem(Context context, AttributeSet attrs, int defStyle) {
    21         super(context, attrs, defStyle);
    22         // TODO Auto-generated constructor stub
    23         inti();
    24     }
    25 
    26     
    27 
    28     public SettingItem(Context context, AttributeSet attrs) {
    29         super(context, attrs);
    30         inti();        
    31        String itemname = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.cskaoyan.mobilemanager", "itemtitle");
    32        desc_checkbox_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.cskaoyan.mobilemanager", "desc_checkbox_on");
    33        desc_checkbox_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.cskaoyan.mobilemanager", "desc_checkbox_off");
    34 
    35        tv_setting_title.setText(itemname);
    36        tv_setting_description.setText(desc_checkbox_off);
    37     }
    38 
    39     public SettingItem(Context context) {
    40         super(context);
    41         // TODO Auto-generated constructor stub
    42         inti();    
    43     }
    44     
    45     
    46     /**
    47      * 这个初始化函数需要完成的使命是:
    48      * 需要在构造这样一个相对不布局的时候,把里面填充三个简单控件
    49      */
    50     private void inti() {
    51         // TODO Auto-generated method stub
    52         
    53 /*        View view=        View.inflate(getContext(), R.layout.setting_item, null);    
    54         this.addView(view);*/
    55         
    56          View v=         View.inflate(getContext(), R.layout.setting_item, this);
    57          
    58          tv_setting_title = (TextView) v.findViewById(R.id.tv_setting_title);
    59          
    60          tv_setting_description = (TextView) v.findViewById(R.id.tv_setting_description);
    61          cb_settingitem = (CheckBox) v.findViewById(R.id.cb_settingitem);
    62 
    63     }
    64     
    65 
    66     
    67     public void setdescription(String text){
    68         tv_setting_description.setText(text);
    69     }
    70 
    71     
    72     public void setdescriptionon(){
    73         tv_setting_description.setText(desc_checkbox_on);
    74     }
    75     
    76     public void setdescriptionoff(){
    77         tv_setting_description.setText(desc_checkbox_off);
    78     }
    79     
    80     public boolean getChecked(){
    81         
    82         return cb_settingitem.isChecked();
    83         
    84     }
    85     
    86     public void setCheck(boolean checked){
    87         cb_settingitem.setChecked(checked);
    88         if (checked) {
    89             setdescriptionon();
    90         }
    91         else {
    92             setdescriptionoff();
    93         }
    94         
    95     }
    96 
    97 }

    3,在/res/values/ 文件夹下建立 attrs.xml 对自定义控件设置需要的属性

    1 <?xml version="1.0" encoding="utf-8"?>
    2 <resources>
    3         <declare-styleable name="com.cskaoyan.mobilemanager.ui.SettingItem">
    4              <attr name="itemtitle" format="string"/>
    5              <attr name="desc_checkbox_on" format="string"/>
    6              <attr name="desc_checkbox_off" format="string"/>
    7         </declare-styleable>
    8 </resources>

      在别的布局文件中设定自定义控件的属性值后, SettingItem.java中就会通过 tv_setting_title.setText(itemname)    tv_setting_description.setText(desc_checkbox_off)将setting_item.xml中控件的文本显示进行设置,实质上最终是设置了setting_item.xml中的控件属性

    1      <com.cskaoyan.mobilemanager.ui.SettingItem
    2           android:id="@+id/settingitem_autoupdate"
    3           android:layout_width="fill_parent"
    4           android:layout_height="wrap_content"
    5           cskaoyan:itemtitle="自动更新"
    6           cskaoyan:desc_checkbox_on="自动更新开启"
    7           cskaoyan:desc_checkbox_off="自动更新关闭"/>

    也可以在SettingItem.java写好一些方法让其他类来调用达到修改的目的

     1     public void setdescription(String text){
     2         tv_setting_description.setText(text);
     3     }
     4 
     5     
     6     public void setdescriptionon(){
     7         tv_setting_description.setText(desc_checkbox_on);
     8     }
     9     
    10     public void setdescriptionoff(){
    11         tv_setting_description.setText(desc_checkbox_off);
    12     }

    物随心转,境由心造,一切烦恼皆由心生。
  • 相关阅读:
    各种算法时空复杂度
    Python文本处理(1)
    数学之路(3)-机器学习(3)-机器学习算法-欧氏距离(3)
    为什么要选择cdn加速
    数据库中操作XML(openXML)
    HDU 3308 LCIS
    Android有效解决加载大图片时内存溢出的问题
    Pathchirp—有效的带宽估计方法(二)
    php三元运算
    C# MVC 自学笔记—4 添加视图
  • 原文地址:https://www.cnblogs.com/woodrow2015/p/4572817.html
Copyright © 2011-2022 走看看