<span style="font-family:Arial;font-size:14px;">自定义一个自定义的控件并且自定义属性(如下图)
<img src="https://img-blog.csdn.net/20150609223305433?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHVoZW5nZGE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
第一步:单独的布局文件setting_item_view.xml
<?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="68dip">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dip"
android:layout_marginLeft="10dip"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_below="@id/tv_title"
android:textColor="#000000"
android:textSize="18sp"/>
<CheckBox
android:id="@+id/cb_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"/>
<View
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_alignParentBottom="true"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="0.2dip"/>
</RelativeLayout>
第二步 在/Mobile_Safe/res/values 新建一个attrs文件夹再新建一个attrs.xml文件
(找到目录platforms\android-16\data\res\values\attrs.xml,参照TextView讲解)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 为自定义的控件添加属性 -->
<declare-styleable name="SettingItemView">
<attr name="title" format="string" />
<attr name="desc_on" format="string" />
<attr name="desc_off" format="string" />
</declare-styleable>
</resources>
第三步:新建一个类SettingItemView加载第一步的布局文件
继承相对布局,或者线性布局 或者其他的ViewGroup;重写三个构造方法,
并在两个参数的构造方法中获取自定义控件的属性值
package com.example.mobile_safe.ui;
import com.example.mobile_safe.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 SettingItemView extends RelativeLayout {
private CheckBox cb_status;
private TextView tv_desc;
private TextView tv_title;
private String mDescOn ;
private String mDescOff;
public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
iniView(context);
// TODO Auto-generated constructor stub
}
private void iniView(Context context){
//加载布局(用到了inflate方法)
View.inflate(context, R.layout.setting_item_view, this);
cb_status = (CheckBox) this.findViewById(R.id.cb_status);
tv_desc = (TextView) this.findViewById(R.id.tv_desc);
tv_title = (TextView) findViewById(R.id.tv_title);
}
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
iniView(context);
//获取自定义的属性值
String title = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.mobile_safe", "title");
mDescOn = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.mobile_safe", "desc_on");
mDescOff = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.mobile_safe", "desc_off");
tv_title.setText(title);
setDesc(mDescOff);
}
public SettingItemView(Context context) {
super(context);
// TODO Auto-generated constructor stub
iniView(context);
}
/**
* 判断CheckBox的状态
* @return boolean
*/
public boolean isChecked(){
return cb_status.isChecked();
}
/**
* 设置CheckBox的状态
* @param checked
*/
public void setChecked(boolean checked){
if(checked){
setDesc(mDescOn);
}else{
setDesc(mDescOff);
}
cb_status.setChecked(checked);
}
/**
* 设置描述TextView中的文字
* @param text
*/
public void setDesc(String text){
tv_desc.setText(text);
}
}
第四步:直接在布局文件中引用这个自定义控件
参照系统的命名空间写自定义属性的命名空间
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:itwolf="http://schemas.android.com/apk/res/com.example.mobile_safe"
<com.example.mobile_safe.ui.SettingItemView
itwolf:title="设置是否更新"
itwolf:desc_on= "自动更新启动"
itwolf:desc_off="自动更新关闭"
android:id="@+id/siv_as_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.example.mobile_safe.ui.SettingItemView>
</span>
Android自定义控件与属性
最新推荐文章于 2021-05-26 22:37:10 发布