Android之路 7. 自定义控件
来源:互联网 发布:怎么做 数据质量管控 编辑:程序博客网 时间:2024/06/12 18:59
常用控件和布局的继承结构
自定义控件
1.引入布局
新建一个布局title.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/title1"> <Button android:id="@+id/title_back" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:background="@drawable/title1" android:text="Back" android:textColor="#fff"/> <TextView android:id="@+id/title_text" android:layout_width="0dp" 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_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:background="@drawable/title1" android:text="Edit" android:textColor="#fff"/></LinearLayout>
background用于为布局或者控件指定一个背景,可以使用颜色或者图片来进行填充。
layout_margin指定控件在上下左右方向上的偏移的距离,当然也可以使用layout_marginLeft或者layout_marginTop等属性来单独指定控件在某个方向上偏移的距离。
修改activity_main.xml中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/title"/></LinearLayout>
在MainActivity中将系统自带的标题栏隐藏掉
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar=getSupportActionBar(); if (actionBar != null){ actionBar.hide(); } }
自定义控件
为了防止需要在每一个活动中为相同的控件单独编写一次事件注册的代码
新建TitleLayout继承自LinearLayout,让它成为自定义的标题栏控件
public class TitleLayout extends LinearLayout{ public TitleLayout(Context context, AttributeSet attributeSet){ super(context,attributeSet); LayoutInflater.from(context).inflate(R.layout.title,this); }}
重写了LinearLayout中带有两个参数的构造函数,在布局中引用TitleLayout控件就会调用这个构造函数,然后借助LayoutInflater来实现对标题栏布局的动态加载。通过LayoutInflater的from()方法可以构建出一个LayoutInflater对象,然后调用inflate()方法就可以实现动态加载一个布局文件,inflate()方法接收两个参数,第一个参数是要加载的布局文件的id,第二个参数是给加载好的布局再添加一个父布局。
自定义控件创建好后,在布局文件activity_main.xml中中添加这个自定义控件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.amanda.uicustomviews.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout>
添加自定义控件和添加普通控件的方式基本是一样的,只不过在添加自定义控件的时候需要指明空间的完整类名,包名不可省略
为标题栏中的按钮注册点击事件,修改TitleLayout中的代码:
public class TitleLayout extends LinearLayout{ public TitleLayout(Context context, AttributeSet attributeSet){ super(context,attributeSet); 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() { @Override public void onClick(View view) { ((Activity) getContext()).finish(); } }); titleEdit.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Toast.makeText(getContext(),"You clicked Edit button",Toast.LENGTH_SHORT).show(); } }); }}
阅读全文
0 0
- Android之路 7. 自定义控件
- Android之自定义控件
- Android之自定义控件
- android之自定义控件
- Android自定义控件之自定义日历控件
- Android自定义控件之自定义组合控件
- Android 自定义控件之自定义组合控件
- Android自定义控件之自定义组合控件
- Android初级之路01:自定义控件
- 【Android成长之路】创建自定义控件
- Android开发学习之路-自定义控件
- Android自定义控件之TextView
- Android自定义控件之TextView
- Android自定义控件之AlertDialog
- android 自定义控件之二
- android自定义控件之radioButton
- Android自定义控件之TextView
- android自定义控件之edittext
- Arduino电路图教程
- java学习笔记-this关键字
- OS学习笔记——处理器调度1
- linux下的redis扩展安装以及使用
- spring配置文件中classpath与classpath* 的区别
- Android之路 7. 自定义控件
- 关于shiro同一帐号同一时刻多处登录问题
- 7天吃透这3大新兴互金黑科技,做稀缺型互金产品经理
- java10个常用小方法.字符串转日期,统计字符串出现频率等.
- CSS在移动端隐藏滚动条/自定义滚动条(scrollbar的各种属性)
- 2017产品经理大会|人人都是产品经理联合腾讯大学,携手16位大咖解码未来产品经理,5折抢门票
- 落地页设计有问题,无法达成转化,如何修改?
- Python编程:从入门到实践2
- 以我对你的了解,我反对你来互联网工作