博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android自定义组件之简单组合
阅读量:4683 次
发布时间:2019-06-09

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

Android自定义控件有两种,一种是组合。比如一个linearlayout 里面有textview,imageview.

这样的好处是,写一个就可以多处使用。

  1. view_image_and_button.xml
    你的组合控件的布局文件

2.自定义属性

app\src\main\res\values\attrs.xml

 

注释:

参考:

  1. reference:参考某一资源ID。
    示例:
  1. color:颜色值。
  1. boolean:布尔值。
示例: [java] 复制代码 代码如下:
  1. dimension:尺寸值。
示例: [java] 复制代码 代码如下:
  1. float:浮点值。
示例: [java] 复制代码 代码如下:
  1. integer:整型值。
示例: [java] 复制代码 代码如下:
  1. string:字符串。
示例: [java] 复制代码 代码如下:
  1. fraction:百分数。
示例: [java] 复制代码 代码如下:
  1. enum:枚举值。
示例: [java] 复制代码 代码如下:
  1. flag:位或运算。
示例: [java] 复制代码 代码如下:

11.多类型。

示例: [java] 复制代码 代码如下:

3.代码

import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import com.chinaCEB.cebActivity.R;/** * Created by Administrator on 2016/6/22. */public class ImageAndButton extends LinearLayout {
private ImageView imageView; private TextView textView; public ImageAndButton(Context context) { super(context); } public ImageAndButton(Context context, AttributeSet attrs) { super(context, attrs); LinearLayout linearLayout= (LinearLayout) LayoutInflater.from(context).inflate(R.layout.view_image_and_button,this,true); imageView = (ImageView) linearLayout.findViewById(R.id.img_top); textView = (TextView) linearLayout.findViewById(R.id.tv_bottom); TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.ImageBtnWithText); textView.setText(typedArray.getText(R.styleable.ImageBtnWithText_text)); imageView.setImageResource(typedArray.getResourceId(R.styleable.ImageBtnWithText_src,R.mipmap.exit)); } public ImageAndButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public void setImageView(int resourseId){ imageView.setImageResource(resourseId); } public void setTextView(String string){ textView.setText(string); }}

4.使用:

注意:要加上下面这句:
xmlns:xinyu=”http://schemas.android.com/apk/res-auto”

转载于:https://www.cnblogs.com/caoxinyu/p/6647846.html

你可能感兴趣的文章
中国象棋程序的设计与实现(六)--N皇后问题的算法设计与实现(源码+注释+截图)...
查看>>
mobiscroll 日期问题
查看>>
<jsp:include>和<%@include%>的区别
查看>>
poj 1691 搜索
查看>>
win7/win8下vmware/VirtualBox虚拟网卡显示未识别网络的解决
查看>>
PCA vs Linear Regression 可视化理解
查看>>
python 二维字典
查看>>
编译原理实验一
查看>>
Git for Android Studio 学习笔记
查看>>
pip 警告!The default format will switch to columns in the future
查看>>
Arrays类学习笔记
查看>>
graphite积累(二)
查看>>
[转]JAVA 反射及使用
查看>>
实验吧之【天下武功唯快不破】
查看>>
c# 生成各种标准条码实例
查看>>
MUTABLE和IMMUTABLE集合
查看>>
忘记MySQL root密码重置MySQL root密码
查看>>
mongodump备份数据库
查看>>
浏览器对象模型 BOM
查看>>
linux下内核的配置和编译(2017-1-17)
查看>>