自定义控件+自定义属性的小Demo

来源:互联网 发布:最新网络伤感情歌 编辑:程序博客网 时间:2024/04/28 17:52
自定义了一个控件,并且自定义了一些属性,然后,将一个textview加载到当前的自定义控件中,用自定义的属性对这个textview进行赋值。最后,讲这个textview添加到当前自定义控件中。先上一个最终效果图再说。


详细说一下,实现该效果的具体步骤

1.自定义一个控件。自定义控件的时候,构造方法有三个。

第一个为一个参数,一般在代码中动态new一个对象的时候用到

第二个为两个参数,一般在动态加载xml文件的时候用到,本例用的就是两个参数的构造方法。

第三个为三个构造参数,一般在xml文件中定义了style属性的时候用到。


2.自定义属性

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="mycustomtext">        <!--    format的属性值:string,color,dimension,integer,enum,    reference,float,boolean,fraction,flag        -->        <attr name="mytext" format="string"></attr>        <attr name="mytextsize" format="dimension"></attr>        <attr name="mytextcolor" format="color"></attr>    </declare-styleable></resources>






3.在xml文件中引用自定义控件和自定义属性。这里有一点需要注意:需要自己在添加一个xmlns的命名控件,作为自定义属性的前缀。命名控件的格式为:    xmlns:mycustomtext="http://schemas.android.com/apk/res/com.mycustomview"。。

其中res/目录后面跟上你项目的包名。也就是R文件所在的包名。


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:mycustomtext="http://schemas.android.com/apk/res/com.mycustomview"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${relativePackage}.${activityClass}" >    <com.mycustomview.MyCustomView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        mycustomtext:mytext="这是一个通过自定义内容,颜色,大小的文本框"        mycustomtext:mytextcolor="#F00"        mycustomtext:mytextsize="10sp" >    </com.mycustomview.MyCustomView></RelativeLayout>



4.修改自定义控件的代码,提取到自定义的属性值

package com.mycustomview;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.LinearLayout;import android.widget.TextView;public class MyCustomView extends LinearLayout {public MyCustomView(Context context, AttributeSet attrs) {super(context, attrs);//取得自定义属性的值,这些值存在TypedArray这个容器中TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.mycustomtext);//通过getString等方法取出每一个自定义属性的值String text = mTypedArray.getString(R.styleable.mycustomtext_mytext);int textcolor = mTypedArray.getColor(R.styleable.mycustomtext_mytextcolor, 0);float textsize = mTypedArray.getDimension(R.styleable.mycustomtext_mytextsize, 0);//讲另外一个textview通过inflate方法,取得这个textview的对象,并且用得到的自定义属性值进行赋值。View view = LayoutInflater.from(getContext()).inflate(R.layout.text_item, null);TextView textView = (TextView) view.findViewById(R.id.textview);textView.setText(text);textView.setTextColor(textcolor);textView.setTextSize(textsize);//讲用自定义属性值赋值好后的textview添加到当前自定义控件中。this.addView(view);}}



5.MainActivity中代码不变,默认代码


package com.mycustomview;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}


源码下载地址(最近缺钱,收一个小币):点击打开链接






0 0
原创粉丝点击