Android 自定义View (0)
来源:互联网 发布:eviews软件中文版下载 编辑:程序博客网 时间:2024/05/20 02:22
虽然我们无法改变人生,但可以改变人生观。虽然我们无法改变环境,但我们可以改变心境。
本讲内容:自定义控件的两种方法
一、引入布局(include)可以多次反复调用
示例一:
下面是res/layout/title.xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/title_bg" > <Button android:id="@+id/title_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:background="@drawable/back_bg" 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="This is Title" android:textColor="#fff" android:textSize="25sp" /> <Button android:id="@+id/title_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:background="@drawable/edit_bg" android:text="Edit" android:textColor="#fff" /></LinearLayout>
下面是res/layout/activity_main.xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" > <include layout="@layout/title"/></LinearLayout>
二、创建自定义控件
引入布局解决了重复编写布局代码问题,但是如果布局中有一些控件要求能够响应事件,明显解决不了重复代码。
示例二:
下面是res/layout/title.xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/title_bg" > <Button android:id="@+id/title_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:background="@drawable/back_bg" 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="This is Title" android:textColor="#fff" android:textSize="25sp" /> <Button android:id="@+id/title_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" android:background="@drawable/edit_bg" android:text="Edit" android:textColor="#fff" /></LinearLayout>
下面是TitleLayout.java文件
public class TitleLayout extends LinearLayout {private Button titleBack;private Button titleEdit;/*当布局中引入TitleLayout控件就会调用带有两个参数的构造函数,在构造函数中需要对标题栏布局进行动态加载,通过LayoutInflater的from()方法可以构建出一个LayoutInflater对象,然后调用inflate()方法就可以动态加载一个布局文件,inflate()方法接收两个参数,第一个参数是要加载的布局文件 的id,第二个参数是人加载好的布局再添加一个父布局,这里我 想要指定为TitleLayout于是直接传入this*/public TitleLayout(Context context, AttributeSet attrs) {super(context, attrs);LayoutInflater.from(context).inflate(R.layout.title, this);titleBack = (Button) findViewById(R.id.title_back);titleEdit = (Button) findViewById(R.id.title_edit);titleBack.setOnClickListener(new OnClickListener() {public void onClick(View v) {//销毁当前的活动((Activity)getContext()).finish();}});titleEdit.setOnClickListener(new OnClickListener() {public void onClick(View v) {Toast.makeText(getContext(), "You clicked Edit Button", Toast.LENGTH_LONG).show();}});}}
下面是res/layout/activity_main.xml 布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" > <com.example.text.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout>
Take your time and enjoy it 路过的、学习过的请留个言,顶个呗~~
1 0
- Android 自定义View (0)
- Android View---自定义View
- Android View---自定义View
- Android 自定义View 之 自定义View属性
- 【自定义View系列】android自定义View概述
- Android自定义view自定义属性
- Android自定义控件 -- 自定义View
- android自定义view(自定义数字键盘)
- Android自定义View-自定义属性
- Android自定义View-自定义属性
- Android 自定义View
- Android 自定义 View
- android自定义View
- Android 中自定义 view
- android 自定义view组件
- Android 自定义 View
- android 自定义view
- Android:如何自定义View
- 二分查找
- js闭包
- 【题解】Binary Tree Right Side View
- 一网打尽当下NoSQL类型、适用场景及使用公司
- raspberrypi 2 共享电脑网络
- Android 自定义View (0)
- python的cls,self,classmethod,staticmethod
- Java正则表达式总结
- 关于VS2010编译CERTI3.4.3中的error MSB6006问题的处理
- 【Opencv】大律法求阈值+分割图像计算像素比
- 12款很棒的浏览器兼容性测试工具
- c语言009---作用域,生命周期,内存数据存储
- T001_UT001_0013
- HDU 3577 Fast Arrangement