`

android复合控件

 
阅读更多
自定义组合控件,用来复用
其一:
首先看布局文件:
<?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
分享到:
评论

相关推荐

    Android复合控件学习之自定义标题栏

    Android复合控件学习之自定义标题栏;学习自定义属性、接口回调等知识点

    Android自定义复合控件

    通过本示例,初学者可以快速掌握如何自定义Android复合控件的技巧

    Android自定义复合控件实现通用标题栏

    主要为大家详细介绍了Android自定义复合控件实现通用标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    Android自定义控件-常用标题栏

    现在我们公司得app都是差不多得title,所以抽时间自己撸了个常用得标题栏控件,直接在布局中使用。 不过本人还是推荐使用官方toolbar 等官方 新控件 ,效果太炫了。无奈设计要求这样,所以在这个公司干,为了开发...

    Android 图片+文字复合控件,有选中效果

    自定义复合控件,支持选中效果及点击事件,非常简单的东西,分享一下,一起学习,欢迎指教

    MyCompoundView自定义复合控件

    复合控件的创建及使用 详见博文http://blog.csdn.net/a87b01c14/article/details/50350405

    《Android 群英传》读书笔记:自定义 View 之创建复合控件

    《Android 群英传》读书笔记:自定义 View 之创建复合控件

    Android图表控件

    相当漂亮的图表控件。包含动画自定义,复合图表,有很强的扩展性。

    《Android群英传》完整扫描版

    图书简介: 本书从由总到分,让读者从整体上把握Android体系结构,融入“群英传”这一故事情节,针对...3.6.2 创建复合控件 43 3.6.3 重写View来实现全新的控件 51 3.7 自定义ViewGroup 54 .........................

    CaptionBar控件控件设计源码

    复合控件CaptionBar主要是针对DataGridView控件而设计,用于灵活定位网格数据和同步显示信息。

    Android开发案例驱动教程 配套代码

    6.2.3 复合类型 105 6.2.4 自定义显示位置Toast 106 6.3 对话框 107 6.3.1 文本信息对话框 107 6.3.2 简单列表项对话框 109 6.3.3 单选项列表项对话框 111 6.3.4 复选框列表项对话框 113 6.3.5 复杂布局列表...

    Android高级编程--源代码

    4.4.2 创建复合控件 83 4.4.3 创建定制的Widget和控件 86 4.4.4 使用定制的控件 97 4.5 创建和使用菜单 98 4.5.1 Android菜单系统简介 98 4.5.2 定义活动的菜单 100 4.5.3 动态更新菜单项 102 4.5.4 处理...

    《Android高级编程》

    4.2 View简介 4.2.1 使用View创建Activity(活动)用户界面 4.2.2 Android Widget工具箱 4.3 布局简介 4.4 创建新的View 4.4.1 修改现有的View 4.4.2 创建复合控件 4.4.3 创建定制的Widget和控件 4.4.4 使用定制的控件...

    Android画个时钟玩玩

    主要向大家介绍了Android画时钟的方法,内容很详细,分享了每一个制作步骤,感兴趣的小伙伴们可以参考一下

    android-toggle-button:适用于 Android 的自定义和可配置动画切换按钮

    Android 自定义切换按钮 获取涵盖此 repo 中代码的教程 通常在创建 Android 应用程序的过程中,开发人员需要创建一个设置页面。... 根据 Android 文档,复合控件允许您“将一个由一组现有控件组成的可

    Android高级编程 part1

     4.4.2 创建复合控件  4.4.3 创建定制的Widget和控件  4.4.4 使用定制的控件  4.5 创建和使用菜单  4.5.1 Android菜单系统简介  4.5.2 定义活动的菜单  4.5.3 动态更新菜单项  4.5.4 处理菜单选择  4.5.5 ...

    Android高级编程 part2

     4.4.2 创建复合控件  4.4.3 创建定制的Widget和控件  4.4.4 使用定制的控件  4.5 创建和使用菜单  4.5.1 Android菜单系统简介  4.5.2 定义活动的菜单  4.5.3 动态更新菜单项  4.5.4 处理菜单选择  4.5.5 ...

    android群雄传

    3.6.2 创建复合控件 43 3.6.3 重写View来实现全新的控件 51 3.7 自定义View Group 54 3.8 事件拦截机制分析 59 第4章 List View使用技巧 65 4.1 List View常用优化技巧 66 4.1.1 使用View Holder模式提高效率 ...

    Android高级编程.pdf

    4.4.2 创建复合控件 4.4.3 创建定制的Widget和控件 4.4.4 使用定制的控件 4.5 创建和使用菜单 4.5.1 Android菜单系统简介 4.5.2 定义活动的菜单 4.5.3 动态更新菜单项 4.5.4 处理菜单选择 4.5.5 子菜单和上下文菜单...

    android UI 布局

    表单控件 复合控件的简单demo 包括listview gridview 等控件的实现

Global site tag (gtag.js) - Google Analytics