android 控件自定义属性,android 组合控件添加自定义属性

实现效果: 第一个效果是在输入框中添加了一个时间选择的按钮,当点击按钮时弹出对应的对话框 第二个效果是在输入框中添加了一个textview,然后textview可以在XML文件中进行配置,主要介绍第二个,其实逻辑都是一样的 1. 在values下面添加文件attrs.xml文件

实现效果:

test.jsp?url=http%3A%2F%2Fimg.blog.csdn.net%2F20141007082337291%3Fwatermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQva2Fud29kZTExMQ%3D%3D%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FSouthEast&refer=http%3A%2F%2Fblog.csdn.net%2Fkanwode111%2Farticle%2Fdetails%2F39851551

test.jsp?url=http%3A%2F%2Fimg.blog.csdn.net%2F20141007082350239%3Fwatermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQva2Fud29kZTExMQ%3D%3D%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FSouthEast&refer=http%3A%2F%2Fblog.csdn.net%2Fkanwode111%2Farticle%2Fdetails%2F39851551

第一个效果是在输入框中添加了一个时间选择的按钮,当点击按钮时弹出对应的对话框

第二个效果是在输入框中添加了一个textview,然后textview可以在XML文件中进行配置,主要介绍第二个,其实逻辑都是一样的

1. 在values下面添加文件attrs.xml文件,然后添加自己自定义属性,如下

2. 因为我是使用的两个控件组合在一起,所以需要定义一个组合控件的样式,在layout下面添加xml,edit_with_unit.xml

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

android:id="@+id/dataText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@drawable/bg_edittext"

android:minEms="4"

android:paddingRight="25dip"

android:textSize="@dimen/text_size_22"

android:textStyle="bold" />

android:id="@+id/dataUnit"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignRight="@id/dataText"

android:gravity="bottom"

android:paddingTop="5dip"

android:textSize="@dimen/text_size_22"

/>

3. 定义自定义控件类,(此处以第二种效果为例,实在是文本框中带控钮,可以直接在此类中做button的监听)

public class UnitView extends LinearLayout {

private String unitStr;

private EditText dataText;

private TextView unitText;

private int textColor;

public UnitView(Context context) {

super(context);

// TODO Auto-generated constructor stub

}

public UnitView(Context context, AttributeSet attrs) {

super(context, attrs);

TypedArray typedArray = context.obtainStyledAttributes(attrs,

R.styleable.unit);

unitStr = typedArray

.getString(R.styleable.unit_unitStr);

textColor = typedArray.getColor(R.styleable.unit_textColor, Color.BLACK);

/*textHintLabel = typedArray

.getString(R.styleable.SelfImageTextAttr_text_hint_label);

image_id = typedArray.getResourceId(

R.styleable.SelfImageTextAttr_image_id, 0);

edttext_inputtype = typedArray

.getString(R.styleable.SelfImageTextAttr_edttext_inputtype);

label_width = Math.round(typedArray.getDimension(

R.styleable.SelfImageTextAttr_label_width, 0));

defaultText = typedArray

.getString(R.styleable.SelfImageTextAttr_default_text);

String txtReadOnly = typedArray

.getString(R.styleable.SelfImageTextAttr_text_readonly);

if (txtReadOnly != null && txtReadOnly.equals("true")) {// comment by

// danielinbiti

isReadOnly = false;

}*/

this.init(context);

typedArray.recycle();

}

public void init(Context context) {

LayoutInflater inflater = LayoutInflater.from(context);

View view = inflater.inflate(R.layout.edit_with_unit, this, true);

unitText = (TextView)view.findViewById(R.id.dataUnit);

dataText = (EditText)view.findViewById(R.id.dataText);

dataText.setTextColor(textColor);

unitText.setText(unitStr);

}

public String getUnitStr() {

return unitStr;

}

public void setUnitStr(String unitStr) {

this.unitStr = unitStr;

}

public EditText getDataText() {

return dataText;

}

public void setDataText(EditText dataText) {

this.dataText = dataText;

}

public TextView getUnitText() {

return unitText;

}

public void setUnitText(TextView unitText) {

this.unitText = unitText;

}

public int getTextColor() {

return textColor;

}

public void setTextColor(int textColor) {

this.textColor = textColor;

}

4. 自定义控件的使用(1).首先必须在需要使用配置的顶部声明一下自定义属性的前缀,
xmlns:前缀名="http://schemas.android.com/apk/res/自定义控件类包名",一定要注意此处是包名,我开始写到了类名,但怎么都找不到我的属性,研究了半天,后来才发现应该是包名。。。伤死心了,浪费了我小半天
<?xml version="1.0" encoding="UTF-8"?>

xmlns:unit="http://schemas.android.com/apk/res/com.lxdz.capacity.activity"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/background_main"

android:orientation="vertical" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginRight="10.0dip"

android:layout_weight="1"

android:imeOptions="actionNext"

android:textSize="@dimen/text_size_20"

unit:unitStr="kV" />

 
 

好了,到些就结束了,我还没怎么写过技术博客,写的不太好,希望有不明白或者大虾多多指导

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值