其一:
首先看布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_below="@+id/iv" android:text="@string/bless" /> </RelativeLayout>
一张图片,一行文字。
然后看定义的控件:
public class ImageBtn extends LinearLayout { private ImageView iv; private TextView tv; private LayoutInflater mInflater; public ImageBtn(Context context) { this(context, null); } public ImageBtn(Context context, AttributeSet attrs) { super(context, attrs); mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mInflater.inflate(R.layout.birth_cloud_item, this, true); } protected void onFinishInflate() { super.onFinishInflate(); iv = (ImageView) findViewById(R.id.iv); tv = (TextView) findViewById(R.id.tv); } public void setImageResource(int resId) { iv.setImageResource(resId); } public void setText(int resId) { tv.setText(resId); } }
好了,这样你就可以使用了。
其二:
自定义一个EditText
首先在drawable定义一个selector,命名为selector_edittext_bg:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/edit_pressed" android:state_focused="true"/> <item android:drawable="@drawable/edit_normal"/> </selector>
然后写个自定义控件的xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <TextView android:id="@+id/tv" android:textColor="#987787" android:layout_width="100dip" android:layout_height="wrap_content" android:textSize="16sp" android:text="@string/phone" /> <EditText android:id="@+id/et" android:layout_width="180dip" android:background="@drawable/selector_edittext_bg" android:layout_height="wrap_content"/> </LinearLayout>
这个就是EditText前面有个说明文字;
接下来是定义一个控件了。
public class MyEditText extends LinearLayout { private EditText et; private TextView tv; private LayoutInflater mInflater; public MyEditText(Context context) { this(context, null); } public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mInflater.inflate(R.layout.myedittext, this, true); } protected void onFinishInflate() { super.onFinishInflate(); et = (EditText) findViewById(R.id.et); tv = (TextView) findViewById(R.id.tv); } public void setText(int resId) { tv.setText(resId); } }
下面是使用方法:
<com.ds.widget.MyEditText android:id="@+id/myedittext" android:layout_width="fill_parent" android:layout_height="wrap_content" />
好了,完工。具体要了解如何定义自己的属性,请看下面:
http://ericchan2012.iteye.com/admin/blogs/1650900
http://ericchan2012.iteye.com/blog/1650754