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
原创粉丝点击