安卓入门---自定义控件
来源:互联网 发布:低音炮哪个牌子好 知乎 编辑:程序博客网 时间:2024/06/04 19:58
控件的继承关系
在此之前先看下关于View的继承关系,其中红色为常用控件。
自定义布局
自定义控件之前先定义控件的布局—-这里做一个导航栏
两个Button和一个TextView
Title.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="wrap_content" android:background="#ff0000"> <Button android:layout_margin="5dp" android:textSize="24sp" android:text="后退" android:background="#000000" android:layout_gravity="center" android:textColor="#fff" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_gravity="center" android:textSize="24sp" android:text="标题" android:gravity="center" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:layout_margin="5dp" android:textSize="24sp" android:text="前进" android:background="#000000" android:layout_gravity="center" android:textColor="#fff" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
在使用的部分引入
<include layout="@layout/title" />
创建自定义控件
真正的自定义控件不只有布局,还有事件响应。所以还要为这个控件添加事件。
新建TitleLayout类继承LinearLayout,并重载时加入布局文件
public class TitleLayout extends LinearLayout { public TitleLayout(Context context, AttributeSet attrs){ super(context,attrs); LayoutInflater.from(context).inflate(R.layout.title,this); }}
如何引入:这里的引入不再是用include,而是引入包名加类名的方式
<com.example.administrator.uicustomviews.TitleLayout android:layout_height="wrap_content" android:layout_width="match_parent" ></com.example.administrator.uicustomviews.TitleLayout>
接下来为两个Button注册点击事件
public TitleLayout(Context context, AttributeSet attrs){ super(context,attrs); LayoutInflater.from(context).inflate(R.layout.title,this); Button left=(Button) findViewById(R.id.left); Button right=(Button) findViewById(R.id.right); left.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((Activity) getContext()).finish(); } }); right.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(),"right",Toast.LENGTH_LONG).show(); } }); }
测试
0 0
- 安卓入门---自定义控件
- 自定义安卓控件
- 安卓----自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件,自定义控件属性
- 安卓自定义日历控件
- 安卓自定义日历控件
- 安卓自定义控件 - 进度条
- 安卓时间控件自定义
- 安卓自定义组合控件
- 安卓自定义刻度盘控件
- 安卓自定义搜索控件
- viewDidLoad调用animateWithDuration无效
- 如何在一个Activity里销毁另外一个Activity
- 2016 js conf
- NTP 时间同步 配置
- 自定义progress
- 安卓入门---自定义控件
- 串行通信里的异步通信与同步通信的区别
- Processing Images(处理图像)留给自己看的
- gson使用
- Android构建过程分析
- iPhone7发布首日,湖南联通快捷预约通道火热开启!
- <自己动手写CPU>之一虚拟Ubuntu安装运行
- hibernate学习笔记
- 建议132:考虑用类名作为属性名