Android 之 自定义标签 和 自定义组件
1 自定义标签
mm_tag.xml (切记不可出现大写,只能是 小写字母、数字、下划线)
第一步: 自定义 标签
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="GridItem">
<attr name="bkground" format="reference|color"/>
<attr name="text1" format="string"/>
<attr name="text2" format="string"/>
<attr name="image" format="reference|integer"/>
</declare-styleable>
</resources>
//属性定义:
< declare -styleable name = "名称" >
<attr name = "orientation" >
<enum name = "horizontal" value = "0" />
<enum name = "vertical" value = "1" />
</attr>
</ declare -styleable> //属性使用:
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
</LinearLayout>
//属性定义:
< declare -styleable name = "名称" >
<attr name = "windowSoftInputMode" >
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
</attr>
</ declare -styleable> //属性使用:
<activity
android: name = ".StyleAndThemeActivity"
android:label = "@string/app_name"
android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden" >
<intent-filter>
< action android: name = "android.intent.action.MAIN" />
<category android: name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//属性定义:
< declare -styleable name = "名称" >
<attr name = "background" format = "reference|color" />
</ declare -styleable> //属性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android: background = "@drawable/图片ID|#00FF00" />
第二步: 在自定义组件中获得标签传回的数据
xmlns:griditem = "http://schemas.android.com/apk/res/com.mm.template"
//对比下 android 的命名空间:
xmlns:android = "http://schemas.android.com/apk/res/android"
< com.mm.template.GridItem
griditem:image = "@drawable/mm_1"
android:padding = "5dp"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_weight = "1"
griditem:bkground = "@color/orange"
griditem:text1 = "Android" griditem:text2 = "手机开发" />
griditem:image = "@drawable/mm_1"
griditem:bkground = "@color/orange"
griditem:text1 = "Android"
griditem:text2 = "手机开发"
public GridItem(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedarray=context.obtainStyledAttributes(attrs, R.styleable.GridItem);
bk_color =typedarray.getResourceId(R.styleable.GridItem_bkground, R.color.burlywood);
text1 =typedarray.getString(R.styleable.GridItem_text1);
text2 =typedarray.getString(R.styleable.GridItem_text2);
image=typedarray.getDrawable(R.styleable.GridItem_image);
typedarray.recycle();
view=LayoutInflater.from(context).inflate(R.layout.mm_grid_item, this,true);
layout=(LinearLayout)view.findViewById(R.id.item_layout);
textview1=(TextView)view.findViewById(R.id.text1);
textview2=(TextView)view.findViewById(R.id.text2);
imageview=(ImageView)view.findViewById(R.id.imageview);
layout.setBackgroundResource(bk_color); //设置背景色
textview1.setText(text1); //设置第一行文字
textview2.setText(text2); //设置第二行文字
imageview.setImageDrawable(image); //设置图标
}2 自定义组件
package com.mm.template; import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView; public class GridItem extends LinearLayout { private int bk_color; //背景色
private String text1; //第一行文字
private String text2; //第二行文字
private Drawable image; //图标 private LinearLayout layout;
private TextView textview1;
private TextView textview2;
private ImageView imageview; private View view; public GridItem(Context context, AttributeSet attrs) {
super(context, attrs); TypedArray typedarray=context.obtainStyledAttributes(attrs, R.styleable.GridItem); bk_color =typedarray.getResourceId(R.styleable.GridItem_bkground, R.color.burlywood);
text1 =typedarray.getString(R.styleable.GridItem_text1);
text2 =typedarray.getString(R.styleable.GridItem_text2);
image=typedarray.getDrawable(R.styleable.GridItem_image); typedarray.recycle(); view=LayoutInflater.from(context).inflate(R.layout.mm_grid_item, this,true); layout=(LinearLayout)view.findViewById(R.id.item_layout);
textview1=(TextView)view.findViewById(R.id.text1);
textview2=(TextView)view.findViewById(R.id.text2);
imageview=(ImageView)view.findViewById(R.id.imageview); layout.setBackgroundResource(bk_color); //设置背景色
textview1.setText(text1); //设置第一行文字
textview2.setText(text2); //设置第二行文字
imageview.setImageDrawable(image); //设置图标
} }
这个自定义组件 GridItem 用到的布局文件
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android: id = "@+id/item_layout"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
android: background = "@color/black"
android:padding = "3dp"
android:paddingLeft = "6dp"
tools:ignore = "HardcodedText,ContentDescription" >
< TextView
android: id = "@+id/text1"
android:layout_weight = "1"
style = "@style/MM_TextView" />
< TextView
android: id = "@+id/text2"
android:layout_weight = "1"
android:textSize = "12sp"
style = "@style/MM_TextView" />
< ImageView
android: id = "@+id/imageview"
android:layout_width = "wrap_content"
android:layout_height = "0dp"
android:layout_gravity = "right"
android: src = "@drawable/mm_title_1"
android:layout_weight = "2"
android:layout_marginTop = "10dp"
android:scaleType = "fitCenter" /> <!--图片缩放
android:scaleX="0.8"
android:scaleY="0.8" --> </ LinearLayout >
3 使用方法
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
xmlns:griditem = "http://schemas.android.com/apk/res/com.mm.template"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android: background = "@color/white"
android:orientation = "vertical"
tools:ignore = "HardcodedText,ContentDescription,NestedWeights" > <!-- Head start -->
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "44dp"
android:orientation = "horizontal"
android:padding = "10dp"
android: background = "@color/red" >
< ImageView
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android: src = "@drawable/mm_title_1" />
< TextView
android:layout_width = "0dp"
android:layout_height = "match_parent"
android:layout_weight = "1"
android:gravity = "center"
android: text = "测试案例"
android:textStyle = "bold"
android:textSize = "16sp"
android:textColor = "@android:color/white" />
< ImageView
android:layout_width = "wrap_content"
android:layout_height = "match_parent"
android: src = "@drawable/mm_title_2" />
</ LinearLayout >
<!-- Head end --> <!-- Search start-->
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "36dp"
android:orientation = "vertical"
android:paddingTop = "3dp"
android:layout_margin = "8dp" >
< EditText
android: id = "@+id/search_edit"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:drawableLeft = "@drawable/mm_search"
android: background = "@drawable/mm_shape_editview"
android:hint = "请输入关键字"
android:textSize = "16sp"
android:textColorHint = "@color/darkgray"
android:textStyle = "bold"
android:padding = "6dp" />
</ LinearLayout >
<!-- Search end -->
<!-- GridItem start -->
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "0dp"
android:layout_weight = "1"
android:orientation = "horizontal"
android:layout_margin = "10dp" >
< com.mm.template.GridItem
griditem:image = "@drawable/mm_1"
android:padding = "5dp"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_weight = "1"
griditem:bkground = "@color/orange"
griditem:text1 = "Android"
griditem:text2 = "手机开发" />
< com.mm.template.GridItem
griditem:image = "@drawable/mm_2"
android:padding = "5dp"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_weight = "1"
griditem:bkground = "@color/blueviolet"
griditem:text1 = "C++"
griditem:text2 = "编程语言" />
< com.mm.template.GridItem
griditem:image = "@drawable/mm_3"
android:padding = "5dp"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_weight = "1"
griditem:bkground = "@color/blue"
griditem:text1 = "服务端"
griditem:text2 = "后台开发" />
</ LinearLayout >
<!-- GridItem end --> </ LinearLayout >
4 结果展示

Android 之 自定义标签 和 自定义组件的更多相关文章
- 写一个可插入自定义标签的 Textarea 组件
- “插入自定义标签是什么鬼?” - “比如你要插入一个<wise></wise>的标签...” - “什么情况下会有这种需求?” - “得罪了产品的情况下...” 一.需求背 ...
- Jsp开发自定义标签,自定义标签将字符串转成指定的时间格式显示
本例以将 字符串格式的时间转成指定的时间格式显示. 第一步.定义一个标签处理程序类,需要集成javax.servlet.jsp.tagext.TagSupport,代码如下: import java. ...
- Struts2自定义标签4自定义分页标签
第一步:webroot/web-inf下的str.tld文件 <?xml version="1.0" encoding="UTF-8"?> < ...
- Struts2自定义标签2自定义一个按班级id查询出该班级下的学生,存放进值栈,并遍历出来。
Struts2自定义标签的流程概念: (1)需要两个类:标签类(继承相应的tag类),基本类(继承Component).标签类专门负责从客户端取得用户输入的一些属性,这个普通的jsp自定义标签一样,取 ...
- django-DIL模板自定义过滤器,自定义标签,自定义包含标签
自定义过滤器 DTL模板语言生来只是为了方便的展示信息,所以与编程语言相比显得有点薄弱,有时候不能满足我们的需求.因此django提供了一个接口,让开发者能自定义标签和过滤器. 首先,你需要添加一个t ...
- django之模板系统 --》内容(filter过滤器、tags标签【for、if、with】、母板以及继承、crf_token、注释、组件、静态文件【load static】、get_static_prefix、自定义标签和tag)
常用: Django模板中只需要记两种特殊符号: {{ }}和 {% %} {{ }}表示变量,在模板渲染的时候替换成值,{% %}表示逻辑相关的操作. 变量 {{ 变量名 }} 变量名由字母数字和下 ...
- 杂项-Java:自定义标签
ylbtech-杂项-Java:自定义标签 1.返回顶部 1. 一般我们说自定义标签是指JSP自定义标签.自定义标签在功能上逻辑上与javaBean 类似,都封装Java 代码.自定义标签是可重用的组 ...
- EL函数以及自定义标签的应用
一.EL函数(调用普通类的静态方法) 编写步骤(自定义EL函数的编写步骤即自定义标签的编写步骤): ①编写一个普通的java类,提供一个静态方法,功能自定,例如下: package cn.wzbril ...
- JSTL自定义标签
这节我们总结一下JSTL自定义标签相关内容. 1. 自定义标签简介 自定义标签主要用于移除JSP页面中的Java代码.Jsp页面主要是用来显示给前台的,如果里面有过多的java代码的话,会显得很乱,但 ...
随机推荐
- loop_login.sh
[oracle@ora10g scripts]$ cat loop_login.sh #/bin/bash####################export ORACLE_BASE=/u01/app ...
- 你好,C++(7)第三部分 C++世界众生相 3.2.1 变量的定义与初始化
第3部分 C++世界众生相 在听过了HelloWorld.exe的自我介绍,完成了与C++世界的第一次亲密接触后,大家是不是都急不可待地想要一试身手,开始编写C++程序了呢?程序的两大任务是描述数据和 ...
- 【USACO 2.4.4】回家
[描述] 现在是晚餐时间,而母牛们在外面分散的牧场中. 农民约翰按响了电铃,所以她们开始向谷仓走去. 你的工作是要指出哪只母牛会最先到达谷仓(在给出的测试数据中,总会有且只有一只最快的母牛). 在挤奶 ...
- 【转】火火火火火!看HomeKit如何改变物联网和智能家居?
摘要: 智能家居并非新概念,然而在苹果等巨头插足之前,它却只是一盘散沙,各自为营,苹果又将如何凭借HomeKit构建起拥有统一界面和控制中心的平台来实现各种智能家居设备与应用之间的无缝连接,真正实现智 ...
- CLR via C#可空值类型
我们知道,一个值类型的变量永远不可能为null.它总是包含值类型本身.遗憾的是,这在某些情况下会成为问题.例如,设计一个数据库时,可将一个列定义成为一个32位的整数,并映射到FCL的Int32数据类型 ...
- 用tbody代替div 解决 table tr的隐藏问题
有如下需求,需要控制一个table内几个tr的显示问题.一开始想的方法是在这几个要显示的tr外面套一个div,利用div的display:none属性来解决. 但是后来发现div和tr嵌套的时候会有问 ...
- linux 路由表设置 之 route 指令详解
使用下面的 route 命令可以查看 Linux 内核路由表. # route Destination Gateway Genmask Flags Metric Ref ...
- The app icon set named "AppIcon" did not have any applicable content.
Develop Tools: xCode 5.1 I write a demo for app settings feature. The tutorial url is here. When I a ...
- 转:Netty系列之Netty高性能之道
1. 背景 1.1. 惊人的性能数据 最近一个圈内朋友通过私信告诉我,通过使用Netty4 + Thrift压缩二进制编解码技术,他们实现了10W TPS(1K的复杂POJO对象)的跨节点远程服务调用 ...
- 【Oracle】windows默认共享的打开和关闭?
Windows启动时都会默认打开admin$ ipc$ 和每个盘符的共享,对于不必要的默认共享,一般都会把它取消掉,可当又需要打开此默认共享时,又该从哪里设置呢,一般来说有两个地方,MSDOS命令和计 ...