android 中自定义组件
2013-04-10 13:09
141 查看
1、在开发中我们难免会遇到一些android 自带的组件无法实现的功能,这时候就需要我们重新去定义这些组件,实例实现了一个复合组件(ImageView 与 TextView )的组合,下面就是截图
custom_image_textview_activity.xml
2、这样定义好了之后,我们就可以在程序中直接引用了,跟用普通组件没什么区别
public class ImageAddText extends LinearLayout implements View.OnClickListener { private ImageView img = null; private TextView name = null; public ImageAddText(Context context) { this(context, null); } public ImageAddText(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); vi.inflate(R.layout.custom_image_textview_activity, this, true); this.img = (ImageView) this.findViewById(R.id.img); this.name = (TextView) this.findViewById(R.id.name); this.img.setOnClickListener(this); } public void setText(String str, int id) { this.img.setBackgroundResource(id); this.name.setText(str); } @Override public void onClick(View v) { new AlertDialog.Builder(MainApplication.getMainActivity()).setMessage("测试").create().show(); } }
custom_image_textview_activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
2、这样定义好了之后,我们就可以在程序中直接引用了,跟用普通组件没什么区别
相关文章推荐
- Android中使用XML自定义组件各种状态下的背景图片
- Android自定义组件之自动换行View
- Android 自定义组件_开关控件
- Android控件之自定义组件
- Android中自定义ViewGroup使每行组件数量不确定,并拿到选中数据
- android 打包自己的自定义组件成JAR包
- Android 自定义组件
- Android自定义组件,实现图片和文字同时显示
- android 打包自己的自定义组件成JAR包
- Android自定义组件系列【5】——进阶实践(1)
- 2014-11-8Android学习------深入理解Android中ViewGroup-----自定义组件
- Android-自定义View组件切换Activity界面
- Android自定义组件系列【5】——进阶实践(2)
- 【Android】Android自定义组件
- Android自定义组件系列【7】——进阶实践(4)
- Android开发指南-用户界面-创建自定义组件
- Android中自定义Checkbox组件实例
- android开发游记:自定义图片裁剪组件
- android自定义组件实现方法
- 说说Android 两种为自定义组件添加属性的使用方法和区别 推荐