自定义组合控件,用来复用
其一:
首先看布局文件:
<?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

更多相关文章

  1. imagebutton 带文字
  2. Android(安卓)问题记录
  3. Android(安卓)添加自定义BOOT_COMPLETED广播避免延迟
  4. imagebutton 带文字
  5. Android中各种Span的用法
  6. 【自定义控件】 ------- GridView
  7. Android右箭头的显示文字的View
  8. Android(安卓)自定义控件打造史上最简单的侧滑菜单
  9. Android状态check、focused、pressed、selected小结

随机推荐

  1. [置顶] 第8章android动态调试
  2. Intellij Idea12 旗舰版 安卓(Android)
  3. Android(安卓)更新UI的两种方法——handl
  4. 【Android】新浪微博开放平台V2开发入门
  5. 小记 ScrollView 隐藏 里面空间填充后再
  6. Android(安卓)3.1 r1 API中文文档(6)——Im
  7. PreferenceActivity 参数设置UI的使用
  8. Building Custom Components/建立自定义
  9. Android学习笔记--service部分
  10. WebView的使用,android与JS混编