我想我们在使用一些App的时候,应该不会出现一些“裸控件”的吧。除非是一些系统中的软件,那是为了保持风格的一致性,做出的一些权衡。我这里并非是在指责Android原生的控件不好看,说实在的,我很喜欢Android的一些原生控件。只是有些时候为了风格的一致性,就不得不去花些功夫在美工上。这于美工这一点,我对某讯的产品的确欣赏。下面就让我们开始一点一点学习Android UI编程中的自定义控件。

分析:

自定义控件就点像堆积木,并给它涂上颜色,和功能说明。下面就让我们用一个例子来逐一地简单讨论一下。

示例分析:

效果图展示:

本示例将选取ImageButton来做一个简单地分析。下面先来看看运行效果图:

  

搭建基本雏形:

对于雏形,首先要做的是积木的选择。我们选择的是一个ImageView和一个TextView,上下摆放,然后用一个约束将其绑定在一起。如下所示的代码片段:

1
2
3
4
5
6
7
8
<!--?xml version=1.0 encoding=utf-8?-->
<linearlayout android:gravity="center_vertical" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
 
    <imageview android:id="@+id/imageView1" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:layout_width="wrap_content" android:paddingbottom="5dip" android:paddingtop="5dip" android:src="@drawable/right_icon">
 
    <textview android:id="@+id/textView1" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" android:layout_margintop="5dp" android:layout_width="wrap_content" android:text="确定" android:textcolor="#000000">
 
</textview></imageview></linearlayout>

上面的代码只能让我们得到一个如上所示的中间方形图和下方的文本以及紧贴在这两者边缘的一个约束。

外观设计和功能添加:

外观设计:

现在我们就要进行颜色分配和功能说明了,它被实现在Java代码中了。如下关键代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
     * 设置图片资源
     */
    public void setImageResource(int resId) {
        imageView.setImageResource(resId);
    }
 
    /**
     * 设置显示的文字
     */
    public void setTextViewText(String text) {
        textView.setText(text);
    }

功能添加:

而对于此我们仅仅只是让这个“Button”更好看了一些罢了。于是我们现在还要做另外一件事。例如点击后要有一些指定的、绑定死的、使用这个控件所必然会进行的操作。其实和上面的加外套是一个性质。如下关键代码:

1
2
3
4
5
6
7
8
9
10
@Override
    public void setOnClickListener(OnClickListener l) {
        auxiliaryFunction();
         
        super.setOnClickListener(l);
    }
     
    protected void auxiliaryFunction() {
        Log.i(TAG, log message.);
    }

上面添加的额外功能,我们可以在Log日志中查看是否有真的完成。

既然是自定义,当然这里的ImageButton原始构建不会是Button。如下真相代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class ImageButton extends LinearLayout {
 
    private ImageView imageView;
    private TextView textView;
 
    public ImageButton(Context context) {
        super(context);
    }
 
    public ImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.image_button, this);
        imageView = (ImageView) findViewById(R.id.imageView1);
        textView = (TextView) findViewById(R.id.textView1);
    }
 
    /**
     * 设置图片资源
     */
    public void setImageResource(int resId) {
        imageView.setImageResource(resId);
    }
 
    /**
     * 设置显示的文字
     */
    public void setTextViewText(String text) {
        textView.setText(text);
    }
 
    @Override
    public void setOnClickListener(OnClickListener l) {
        auxiliaryFunction();
         
        super.setOnClickListener(l);
    }
     
    protected void auxiliaryFunction() {
        Log.i(TAG, log message.);
    }
}

使用分析:

1.xml代码中的使用

这里只是有一点需要注意,我们要指明自定义控件的完整路径,如下:

1
<com.demo.customview.imagebutton.widgets.imagebutton android:background="@drawable/custom_button" android:id="@+id/btn_right" android:layout_height="150dp" android:layout_width="150dp"></com.demo.customview.imagebutton.widgets.imagebutton>

2.动作效果配置

对于Button的动作也就是触摸、按下和抬起,对于这三个动作效果的配置需要在res包下的drawable文件夹中去创建(没有这个文件夹就新建一个)。如下代码:

1
2
3
4
5
6
7
8
<!--?xml version=1.0 encoding=utf-8?-->
 
    <item android:drawable="@drawable/button_unpress_background" android:state_focused="true" android:state_pressed="false"></item>
    <item android:drawable="@drawable/button_pressed_background" android:state_pressed="true"></item>
    <item android:drawable="@drawable/button_unpress_background" android:state_focused="false" android:state_pressed="false"></item>
 
</selector>

3.Java代码中的使用

在Java代码的使用与Button无异,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class MainActivity extends Activity {
 
    private ImageButton mImageBtn1;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mImageBtn1 = (ImageButton) this.findViewById(R.id.btn_right);
        mImageBtn1.setTextViewText(确定);
        mImageBtn1.setImageResource(R.drawable.right_icon);
 
        mImageBtn1.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), 点击确定, 0).show();
            }
        });
    }
}
 

结伴旅游,一个免费的交友网站:www.jieberu.com

推推族,免费得门票,游景区:www.tuituizu.com

Android UI编程之自定义控件初步——ImageButton的更多相关文章

  1. Android UI编程(1)——九宫格(GridView)

    (转自:http://blog.csdn.net/Thanksgining/article/details/42968847) 参考博客:http://blog.csdn.net/xyz_lmn/ar ...

  2. Android UI组件之自定义控件实现IP地址控件

    http://www.cnblogs.com/razerlack/p/4273282.html

  3. Android全新UI编程 - Jetpack Compose 超详细教程

    1. 简介 Jetpack Compose是在2019Google i/O大会上发布的新的库.Compose库是用响应式编程的方式对View进行构建,可以用更少更直观的代码,更强大的功能,能提高开发速 ...

  4. Qt on Android 核心编程

    Qt on Android 核心编程(最好看的Qt编程书!CSDN博主foruok倾力奉献!) 安晓辉 著   ISBN 978-7-121-24457-5 2015年1月出版 定价:65.00元 4 ...

  5. GitHub上受欢迎的Android UI Library

    GitHub上受欢迎的Android UI Library 内容 抽屉菜单 ListView WebView SwitchButton 按钮 点赞按钮 进度条 TabLayout 图标 下拉刷新 Vi ...

  6. Android UI相关开源项目库汇总

    最近做了一个Android UI相关开源项目库汇总,里面集合了OpenDigg 上的优质的Android开源项目库,方便移动开发人员便捷的找到自己需要的项目工具等,感兴趣的可以到GitHub上给个st ...

  7. 各种Android UI开源框架 开源库

    各种Android UI开源框架 开源库 转 https://blog.csdn.net/zhangdi_gdk2016/article/details/84643668 自己总结的Android开源 ...

  8. android: 多线程编程基础

    9.1   服务是什么 服务(Service)是 Android 中实现程序后台运行的解决方案,它非常适合用于去执行那 些不需要和用户交互而且还要求长期运行的任务.服务的运行不依赖于任何用户界面,即使 ...

  9. Android 网络编程 Socket

    1.服务端开发 创建一个Java程序 public class MyServer { // 定义保存所有的Socket,与客户端建立连接得到一个Socket public static List< ...

随机推荐

  1. 在react项目当中做导航守卫

    距离上一篇文章,似乎已经过去好久了. 确实是最近相对忙了一点,本身是用vue重构之前一个传统的项目,就自己一个人写.而且,在稍微闲暇之余,想着同时用react也重构一遍,也算是对react的学习吧!毕 ...

  2. ZOOKEEPER进阶

    集群个数: 2n+1,因为集群当宕机大于等于二分之一的机子时,集群选举会失败.故 2n+1台机器和3n台机器可靠性相同 Leader的作用: 为了实现各个节点数据的一致性,需要一个负责协调数据同步的操 ...

  3. swagger 报错打不开

    1.controller中的接口里使用的 qto的数据类型有问题: qo中的字段中缺少:(@JsonProperty(value = "sort"),以及定义的example值的格 ...

  4. 【原创】大数据基础之ETL vs ELT or DataWarehouse vs DataLake

    ETL ETL is an abbreviation of Extract, Transform and Load. In this process, an ETL tool extracts the ...

  5. SQL Join的应用(转)

    INNER JOIN LEFT JOIN RIGHT JOIN OUTER JOIN LEFT JOIN EXCLUDING INNER JOIN RIGHT JOIN EXCLUDING INNER ...

  6. 函数缓存 (Function caching)

    函数返回值缓存是优化一个函数的常用手段.我们可以将函数.输入参数.返回值全部保存起来,当下次以同样的参数调用这个函数时,直接使用存储的结果作为返回(不需要重新计算). 函数缓存允许我们将一个函数对于给 ...

  7. Centos搭建hexo教程

    hexo文档:https://hexo.io/zh-cn/ 1.安装Git # sudo yum install git-core// 查看版本# git version// 输出git versio ...

  8. fastadmin Excel导出时数字被科学计数

    /public/assets/libs/bootstrap-table/dist/extensions/export/bootstrap-table-export.min.js //exportOpt ...

  9. Delphi 媒体播放器控件

    樊伟胜

  10. Linux系统安装常用开发软件

    vim.jdk.tomcat.mysql 安装vim(命令模式=>编辑模式=>底行模式) [root@localhost ~]# yum install vim*结束后一直确认即可,键入y ...