自定义组合控件,用来复用
其一:
首先看布局文件:
<?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. Android 自定义控件打造史上最简单的侧滑菜单
  2. android的map上的浮动菜单以及日历控件
  3. android:shape的使用 (android用xml文件生成图像控件)
  4. Android 添加自定义BOOT_COMPLETED广播避免延迟
  5. 【自定义控件】 ------- GridView
  6. imagebutton 带文字
  7. Android右箭头的显示文字的View
  8. android 自定义View 在布局文件中写法
  9. android 自定义按钮的外边框

随机推荐

  1. Android(安卓)studio4.0 解决导入包Error
  2. selector选择器
  3. Android开发实战三之导入现有Android工程
  4. 2011.06.29(2)——— android 完全退出
  5. eclipse 开发 android 快捷键!
  6. Android(安卓)Studio INSTALL_FAILED_UID
  7. Android开发便签9:在android资源文件中定
  8. 使用android中的aapt查看安卓apk包信息
  9. Android属性系统
  10. android 4.4 设置默认日期格式