博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android进阶之通过自定义属性-自定义更多界面
阅读量:2193 次
发布时间:2019-05-02

本文共 2387 字,大约阅读时间需要 7 分钟。

1 自定义复合控件

当使用简单布局定义界面时,客户端对手机内存的消耗是巨大的,同时过多的布局嵌套让代码显得异常的凌乱。因此,解决方式就是自定义控件。接下来,一步一步来实现自定义界面。

1.1 实现效果

这里写图片描述

1.2 一般步骤

1.2.1 自定义View的一般步骤:

(1)自定义View的属性

(2)在View的构造方法中获得我们自定义的属性
(3)重写onMesure (不是必须的)
(4)重写onDraw
(5)在布局中使用此自定义View

1.2.2 自定义复合控件的一般步骤:

(1)自定义布局(基础)

(2)自定义布局对应的属性(attr.xml)
(3)在View的构造方法中获得“自定义布局的控件”&“自定义的属性”(attr.xml),将attr.xml取得的属性值添加到自定义的控件中绑定。
(4)重写onMesure (不是必须的)
(5)重写onDraw
(6)在布局中使用此自定义View

2 先看一张关系图

这里写图片描述

##3 例子代码讲解

###3.1 attrs.xml

3.2 view_coutom.xml

3.3 MyView.java

/** * Created by Guan on 2015/7/25. */public class MyView extends FrameLayout {    private ImageView mMoreImage;    private TextView mCustomer;    private ImageView mRightImage;    public MyView(Context context) {        super(context);    }    public MyView(Context context, AttributeSet attrs) {        super(context, attrs);        // 1.1 通过布局view_custom.xml自定义每一行的格式        // 1.2 再获取到view,并获取到具体的控件        View view = View.inflate(context, R.layout.view_coutom, this);        mMoreImage = (ImageView) view.findViewById(R.id.iv_more);        mCustomer = (TextView) view.findViewById(R.id.tv_customer);        mRightImage = (ImageView) view.findViewById(R.id.iv_right);		// 2.1 通过布局attr.xml自定义每一行View的属性        // 2.2 通过attr.xml取得declare-styleable集合,从集合里取出相对应的属性值        TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.zView);        Drawable imageMore = typeArray.getDrawable(R.styleable.zView_iconMore);        String customerString = typeArray.getString(R.styleable.zView_textCustomer);        Drawable imageRight = typeArray.getDrawable(R.styleable.zView_iconRight);        // 3.将attr.xml取得的属性值添加到自定义的控件中绑定        mMoreImage.setImageDrawable(imageMore);        mCustomer.setText(customerString);        mRightImage.setImageDrawable(imageRight);        // 关闭资源        typeArray.recycle();    }    // 4.重写onDraw    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);    }}

3.4 activity_main.xml

4 注意

在XML使用该组件的时候一定要为该自定义组件设置一个命名空间。

4.1 eclipse

命名空间写法:xmlns:空间名="http://schemas.android.com/apk/res/自定义组件所在包名"

4.2 android studio

命名空间写法:xmlns:空间名="http://schemas.android.com/apk/res-auto"

例如:xmlns:myandroid="http://schemas.android.com/apk/res-auto"

转载地址:http://vmcub.baihongyu.com/

你可能感兴趣的文章
Intellij IDEA使用(一)—— 安装Intellij IDEA(ideaIU-2017.2.3)并完成Intellij IDEA的简单配置
查看>>
Intellij IDEA使用(二)—— 在Intellij IDEA中配置JDK(SDK)
查看>>
Intellij IDEA使用(三)——在Intellij IDEA中配置Tomcat服务器
查看>>
Intellij IDEA使用(四)—— 使用Intellij IDEA创建静态的web(HTML)项目
查看>>
Intellij IDEA使用(五)—— Intellij IDEA在使用中的一些其他常用功能或常用配置收集
查看>>
Intellij IDEA使用(六)—— 使用Intellij IDEA创建Java项目并配置jar包
查看>>
Eclipse使用(十)—— 使用Eclipse创建简单的Maven Java项目
查看>>
Eclipse使用(十一)—— 使用Eclipse创建简单的Maven JavaWeb项目
查看>>
Intellij IDEA使用(十三)—— 在Intellij IDEA中配置Maven
查看>>
面试题 —— 关于main方法的十个面试题
查看>>
集成测试(一)—— 使用PHP页面请求Spring项目的Java接口数据
查看>>
使用Maven构建的简单的单模块SSM项目
查看>>
Intellij IDEA使用(十四)—— 在IDEA中创建包(package)的问题
查看>>
FastDFS集群架构配置搭建(转载)
查看>>
HTM+CSS实现立方体图片旋转展示效果
查看>>
FFmpeg 命令操作音视频
查看>>
问题:Opencv(3.1.0/3.4)找不到 /opencv2/gpu/gpu.hpp 问题
查看>>
目的:使用CUDA环境变量CUDA_VISIBLE_DEVICES来限定CUDA程序所能使用的GPU设备
查看>>
问题:Mysql中字段类型为text的值, java使用selectByExample查询为null
查看>>
程序员--学习之路--技巧
查看>>