自定义布局控件
来源:互联网 发布:淘宝直播有什么要求 编辑:程序博客网 时间:2024/03/29 01:08
本文介绍一种自定义控件的方法,由控件布局和控件代码2部分组成。效果为一个自定义标题栏,由一个按钮、一个文本、一个按钮组成,并定义了各子件的事件。
一、title布局如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/title_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dip" android:text="Back" android:textColor="#fff" /> <TextView android:id="@+id/title_text" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="Title Text" android:textColor="#fff" android:textSize="24sp" /> <Button android:id="@+id/title_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dip" android:text="Edit" android:textColor="#fff" /></LinearLayout>
二、代码如下。重写构造器,在其中展开布局,找到子控件绑定事件
public class TitleLayout extends LinearLayout {public TitleLayout(Context context, AttributeSet attrs) {super(context, attrs);LayoutInflater.from(context).inflate(R.layout.title, this);Button titleBack = (Button) findViewById(R.id.title_back);Button titleEdit = (Button) findViewById(R.id.title_edit);titleBack.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {((Activity) getContext()).finish();}});titleEdit.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getContext(), "You clicked Edit button",Toast.LENGTH_SHORT).show();}});}}
三、活动中引入自定义控件,要使用完整包名
<com.ui.TitleLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
四、自定义控件也可以在主活动中重新定义子控件的属性及事件
0 0
- 自定义布局控件
- 自定义布局控件
- 【android控件】dialog自定义布局
- 自定义控件--垂直滑动布局
- 创建自定义标题布局控件
- xml布局使用自定义控件
- 自定义控件实现图片布局
- 自定义流式布局控件
- 自定义控件之流式布局
- 自定义组合控件--来自布局
- 悬浮指示器布局-自定义控件
- 自定义控件:流式布局
- 自定义控件格子布局:CellLayout
- Android自定义控件之流布局
- 流式布局(自定义控件)
- UICustomViews 自定义布局和控件
- Android获取dialog自定义布局中的控件
- WPF ListView控件布局自定义示例
- Matlab的unique函数的C++实现
- 约瑟夫环
- 数据表不没有数据的时候,各个统计函数返回的值
- ios新手如何看xcode缓存路径
- [深入理解Java虚拟机]第十二章 Java内存模型与线程-Java内存模型
- 自定义布局控件
- MangoDB环境搭建与学习
- 【C#】表达式
- 赫夫曼树与赫夫曼编码
- 初学Android,字符串,数字,尺寸,数组资源(十二)
- iOS开发25-iOS可视化编程-Interface Builder
- 四元数的转换(一)
- 我的Java学习笔记1——java执行Oracle的sql语句时报错ORA-00911无效字符错误
- 文件读写权限