有时候,可能觉得系统提供的控件太丑,就会需要自定义控件来实现自己想要的效果。

以下主要参考《第一行代码》

1.自定义一个标题栏:

系统自带的标题栏很丑,且没什么大的作用,所以我们之前会在onCreate()中调用requestWindowFeature(Window.FEATURE_NO_TITLE);设置不显示标题栏。

下面自定义一个标题栏,中间显示标题,左右各有一个按钮:

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:orientation="horizontal"

     android:background="#bbbbbb" >

     <Button

         android:id="@+id/btn_back"

         android:text="@string/back"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:layout_margin="5dp"

         android:layout_gravity="left|center_vertical"

         android:textColor="#0099cc"

         android:layout_weight="1"/>

     <TextView

         android:id="@+id/title"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:textSize="20sp"

         android:textColor="#0099cc"

         android:text="@string/this_is_title"

         android:layout_gravity="center"

         android:gravity="center"

         android:layout_weight="2"/>

     <Button

         android:id="@+id/btn_edit"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:text="@string/edit"

         android:layout_margin="5dp"

         android:layout_gravity="right|center_vertical"

         android:textColor="#0099cc"

         android:layout_weight="1"/>

 </LinearLayout>

Activity代码:

 protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);     

            requestWindowFeature(Window.FEATURE_NO_TITLE);

            setContentView(R.layout.title);

 }

运行结果:

(⊙o⊙)…有点丑哈,不过仔细看,还是有点像标题栏的。

2.复用布局代码:

想让这个标题栏应用在以后的每个布局文件,要怎么做呢?

总不能每次都把这些xml代码重写一遍吧。

android布局中提供了类似于c预处理指令#include的<include>标签,可以实现布局代码的复用。

下面新建一个first_layout.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="match_parent"

     android:orientation="vertical" >

     <include layout="@layout/title"/>

     <Button android:id="@+id/btn"

         android:text="@string/i_m_a_button"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:layout_gravity="center_horizontal"/>

 </LinearLayout>

修改setContentView(R.layout.first_layout);

显示结果:

现在Back和Edit按钮都没有任何事件处理的,怎样实现点击Back按钮就结束当前Activity呢?方法跟之前的做法完全一样,使用findViewById()根据id找到Back按钮,然后设置click事件监听即可。

代码如下:

 public class FirstActivity extends Activity {

       @Override

       protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);     

            requestWindowFeature(Window.FEATURE_NO_TITLE);

            setContentView(R.layout.first_layout);

            Button btn = (Button) findViewById(R.id.btn_back);

            btn.setOnClickListener(new OnClickListener() {

                  @Override

                  public void onClick(View v) {

                       // TODO Auto-generated method stub

                       FirstActivity.this.finish();

                  }

            });

       }

 }

布局文件的复用已然通过<include>实现了,但是每次都要重新写事件监听,还是觉得麻烦……到这里一般就会想到抽象出一个自定义类,每次需要的时候,直接使用该自定义类不就行了,其实就是自定义控件的做法了。

3.自定义控件,复用功能代码

TitleLinearLayout.java代码:

 public class TitleLinearLayout extends LinearLayout {

       public TitleLinearLayout(Context context, AttributeSet attrs) {

            super(context, attrs);

            LayoutInflater.from(context).inflate(R.layout.title, this);

            Button btn_back = (Button) findViewById(R.id.btn_back);

            btn_back.setOnClickListener(new OnClickListener() {

                  @Override

                  public void onClick(View v) {

                       // TODO Auto-generated method stub

                       Log.i("clicked","back");

                       ((Activity)getContext()).finish();

                  }

            });

       }

 }

继承自LinearLayout,实现带两个参数的构造方法。在构造方法中,加载布局文件,并对其中的Back按钮进行事件监听设置。

LayoutInflater.from(context).inflate(R.layout.title, this);用于动态加载布局文件。

注意到,Activity中有一个获取LayoutInflater的方法,所以,也可以使用下面一行代码加载布局文件:

((Activity)context).getLayoutInflater().inflate(R.layout.title, this);这种方法,在Activity代码中比较常用,而这里需要进行类型强转,反倒麻烦点,而且不如第一个方法安全。

如何使用自定义的控件呢?

first_layout代码如下:

 <?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="match_parent"

     android:orientation="vertical" >

     <cn.csc.custom_ui.TitleLinearLayout

         android:layout_width="match_parent"

         android:layout_height="wrap_content">

     </cn.csc.custom_ui.TitleLinearLayout>

     <Button android:id="@+id/btn"

         android:text="@string/i_m_a_button"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:layout_gravity="center_horizontal"/>

 </LinearLayout>

说明:

1)在布局文件中,引用自定义的控件,需要使用完整的类限定名,即包名.类名的方式;

2)在定义控件中,设置属性时,使用alt+/进行代码提示补全功能将经常不可用,标签名可以先设置为内置控件,然后进行属性的设置,之后再把标签名改回到自定义的控件的完整限定名即可。

android菜鸟学习笔记13----Android控件(二) 自定义控件简单示例的更多相关文章

  1. Android开发学习笔记-自定义组合控件的过程

    自定义组合控件的过程 1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup:2.实现父类的构造方法.一般来说,需要在构造方法里初始化自定义的布局文件:3.根据一些需要或者需 ...

  2. Android开发学习笔记-自定义组合控件

    为了能让代码能够更多的复用,故使用组合控件.下面是我正在写的项目中用到的方法. 1.先写要组合的一些需要的控件,将其封装到一个布局xml布局文件中. <?xml version="1. ...

  3. WPF-学习笔记 动态修改控件Margin的值

    原文:WPF-学习笔记 动态修改控件Margin的值 举例说明:动态添加一个TextBox到Grid中,并设置它的Margin: TextBox text = new TextBox(); t_gri ...

  4. Android自动化学习笔记之MonkeyRunner:官方介绍和简单实例

    ---------------------------------------------------------------------------------------------------- ...

  5. android菜鸟学习笔记14----Android控件(三) ListView的简单使用

    MVC模式: MVC的基本原理就是通过Controller连接View和Model.当View中所显示的数据发生变化时,会通知Controller,然后由Controller调用Model中的相关方法 ...

  6. android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件

    主要参考<第一行代码> 1.TextView: 功能与传统的桌面应用开发中的Label控件相似,用于显示文本信息 如: <TextView android:layout_width= ...

  7. Android学习笔记_30_常用控件使用

    一.状态栏通知(Notification): 如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息.发送消息的代码如下: public void sendNotice(View v){ int ic ...

  8. android 学习笔记四:控件

    1.android:gravity 指定控件的基本位置,比如居中.居右等位置 Top:顶部 bottom:底部 left:居左 right:居右 center_vertical:垂直居中 center ...

  9. android菜鸟学习笔记30----Android使用百度地图API(一)准备工作及在应用中显示地图

    1.准备工作: 百度地图API是免费开放的,但是需要申请API Key: 1)先注册一个百度开发者帐号 2)进入百度开放服务平台http://developer.baidu.com/ 3)进入LBS云 ...

随机推荐

  1. 洛谷—— P1609 最小回文数

    https://www.luogu.org/problemnew/show/1609 题目描述 回文数是从左向右读和从右向左读结果一样的数字串. 例如:121.44 和3是回文数,175和36不是. ...

  2. spring 基础

    作者:Spring太难链接:https://zhuanlan.zhihu.com/p/38131490来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 目录 Spring ...

  3. Xamarin XAML语言教程Visual Studio中实现XAML预览

    Xamarin XAML语言教程Visual Studio中实现XAML预览 每次通过编译运行的方式查看XAML文件效果,需要花费大量的时间.如果开发者使用XAML对UI进行布局和设计,可以通过预览的 ...

  4. luogu P3376 【模板】网络最大流(no)ek

    题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...

  5. sql server book

    http://www.sqlpassion.at/blog/ http://www.sqlservercentral.com/Books/

  6. MAC - 命令行中用sublime打开指定文件,使用ln命令建立软链接

    眼下sublime是mac下最好的文本编辑软件.常常要使用它打开一些文件,比如html,js,txt,json等文件,可是sublime2默认不支持在命令行下调用.经过研究发现能够用建立软连接的方式调 ...

  7. 微信小程序 - 对象转换成对象数组

    后端传过来的一个个对象 {1,2,3,4},{1,3,5,},{1,3,5} 我们应该转化为数组对象 [{},{},{},{}]  ,最后通过wx:for遍历到页面 示例图:

  8. 利用MFC里面格式化函数也可以实现可变长度的问题

    直接粘代码: 1: CString str1;  //定义两个MFC里面的CString里面的字符串 2: CString str2; 3: str1.Format("(%d)", ...

  9. 跨域用ajax处理并返回处理状态

    <script type="text/javascript">        $(function () {            $("#submitBtu ...

  10. C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志

    C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...