Android自定义组件之简单组合
Android自定义控件有两种,一种是组合。比如一个linearlayout 里面有textview,imageview.
这样的好处是,写一个就可以多处使用。
- view_image_and_button.xml
你的组合控件的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/tv_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="aaa"
/>
</LinearLayout>
2.自定义属性
app\src\main\res\values\attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ImageBtnWithText">
<attr name="text" format="string"/>
<attr name="src" format="reference"/>
</declare-styleable>
</resources>
注释:
参考:http://www.jb51.net/article/32172.htm
- reference:参考某一资源ID。
示例:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
<attr name = "src" format = "reference" />
</declare-styleable>
- color:颜色值。
<declare-styleable name = "名称">
<attr name = "textColor" format = "color" />
</declare-styleable>
- boolean:布尔值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
- dimension:尺寸值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
- float:浮点值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
- integer:整型值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
</declare-styleable>
- string:字符串。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "text" format = "string" />
</declare-styleable>
- fraction:百分数。
示例:
[java]
复制代码 代码如下:
<declare-styleable name="名称">
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
</declare-styleable>
- enum:枚举值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
- flag:位或运算。
示例:
[java]
复制代码 代码如下:
<declare-styleable name="名称">
<attr name="windowSoftInputMode">
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
<flag name = "stateAlwaysHidden" value = "3" />
</attr>
</declare-styleable>
11.多类型。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>
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”
<com.chinaCEB.cebActivity.widget.ImageAndButton
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent"
xinyu:src="@mipmap/menu_item_icon_shake_selected"
xinyu:text="@string/shuifei"
>
</com.chinaCEB.cebActivity.widget.ImageAndButton>
Android自定义组件之简单组合的更多相关文章
- Android自定义组件之自动换行及宽度自适应View:WordWrapView
目的: 自定义一个ViewGroup,里面的子view都是TextView,每个子view TextView的宽度随内容自适应且每行的子View的个数自适应,并可以自动换行 一:效果图 二:代码 整 ...
- Android自定义组件系列【7】——进阶实践(4)
上一篇<Android自定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识,这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpan ...
- Android自定义组件系列【6】——进阶实践(3)
上一篇<Android自定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计划 ...
- Android自定义组件系列【5】——进阶实践(2)
上一篇<Android自定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这一 ...
- Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动
在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...
- Android 自定义组件之如何实现自定义组件
参考链接:http://blog.csdn.net/jjwwmlp456/article/details/41076699 简介 Android提供了用于构建UI的强大的组件模型.两个基类:View和 ...
- Android自定义组件系列【3】——自定义ViewGroup实现侧滑
有关自定义ViewGroup的文章已经很多了,我为什么写这篇文章,对于初学者或者对自定义组件比较生疏的朋友虽然可以拿来主义的用了,但是要一步一步的实现和了解其中的过程和原理才能真真脱离别人的代码,举一 ...
- Android 自定义组件,自定义LinearLayout,ListView等样式的组件
今天讲的其实以前自己用过,就是在网上拿下来的把图片裁剪成圆形的方法,之前的随笔也介绍过的, 用法就是,在布局里写控件或者组件的时候得把从com开始到你写的那个类的所有路径写下来. 至于我们该怎么创建呢 ...
- Android自定义组件
[参考的原文地址] http://blog.csdn.net/l1028386804/article/details/47101387效果图: 实现方式: 一:自定义一个含有EditText和Butt ...
随机推荐
- 用HttpSessionListener统计在线用户或做账号在线人数管理
使用HttpSessionListener接口可监听session的创建和失效 session是在用户第一次访问页面时创建 在session超时或调用request.getSession().inva ...
- SQL查询某一字段重复的数据
查询出重复记录 select * from 数据表 WHERE 重复记录字段 in ( select 重复记录字段 from 数据表 group by 重复记录字段 having count(重复记 ...
- avast从隔离区恢复后,仍无法打开被误杀文件的解决方案
从隔离区中手动恢复后,隔离区中被恢复的文件将不再展示. 此时,如果手动恢复的文件仍无法打开(图标此时也异常),请: 将avast禁用: 将avast启用. 然后尝试重新打开被误隔离并手动恢复的文件.
- javascript中的循环引用对象处理
先说明一下什么是循环引用对象: var a={"name":"zzz"}; var b={"name":"vvv"}; ...
- WIN7如何在任务栏建立我的电脑的快捷图标
1. 在桌面空白处鼠标右击->新建->快捷方式,在弹出的对话框中输入 %SystemRoot%\explorer.exe /E,::{20D04FE0-3AEA-1069-A2D8-08 ...
- iOS 有些库只能在真机上运行,不能在模拟器上运行的解决方式
在开发中,多少肯定会用到第三方的东西,或许大家也和我一样遇到到这样的情况,有些库正好适合自己的需求,但是这个库却只支持真机上运行,在模拟器上编译却不通过, 一般情况下,.a静态包,你刚刚导入的时候,不 ...
- delete分析 引用于 http://www.cnblogs.com/yuzhongwusan/archive/2012/06/14/2549879.html
最近重新温习JS,对delete操作符一直处于一知半解的状态,偶然发现一篇文章,对此作了非常细致深入的解释,看完有茅塞顿开的感觉,不敢独享,大致翻译如下. 原文地址:http://perfection ...
- 我是如何根据豆瓣api来理解Restful API设计的
1.什么是REST REST全称是Representational State Transfer,表述状态转移的意思.它是在Roy Fielding博士论文首次提出.REST本身没有创造新的技术.组件 ...
- 时间戳与QDateTime相互转换
最近项目中需要将日期时间输出到Excel中,程序使用Qt开发,使用第三方库QtXlsx进行Excel读写操作.Excel中第一列为时间,时间间隔为1小时,如图所示. 赋值起始时间stDTime,则后续 ...
- 异构数据库迁移——DATAX
背景 在最近接触到的一个case里面,需要把db2的数据迁移至oracle,客户可接收的停机时间为3小时. 同步方式的比较 一说到停机时间,大家第一时间想到Oracle公司的GoldenGate实时同 ...