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

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

  1. 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

  1. reference:参考某一资源ID。

    示例:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
<attr name = "src" format = "reference" />
</declare-styleable>
  1. color:颜色值。
<declare-styleable name = "名称">
<attr name = "textColor" format = "color" />
</declare-styleable>
  1. boolean:布尔值。
示例:
[java]
复制代码 代码如下: <declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
  1. dimension:尺寸值。
示例:
[java]
复制代码 代码如下: <declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
  1. float:浮点值。
示例:
[java]
复制代码 代码如下: <declare-styleable name = "名称">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
  1. integer:整型值。
示例:
[java]
复制代码 代码如下: <declare-styleable name = "名称">
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
</declare-styleable>
  1. string:字符串。
示例:
[java]
复制代码 代码如下: <declare-styleable name = "名称">
<attr name = "text" format = "string" />
</declare-styleable>
  1. fraction:百分数。
示例:
[java]
复制代码 代码如下: <declare-styleable name="名称">
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
</declare-styleable>
  1. enum:枚举值。
示例:
[java]
复制代码 代码如下: <declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
  1. 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自定义组件之简单组合的更多相关文章

  1. Android自定义组件之自动换行及宽度自适应View:WordWrapView

    目的: 自定义一个ViewGroup,里面的子view都是TextView,每个子view  TextView的宽度随内容自适应且每行的子View的个数自适应,并可以自动换行 一:效果图 二:代码 整 ...

  2. Android自定义组件系列【7】——进阶实践(4)

    上一篇<Android自定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识,这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpan ...

  3. Android自定义组件系列【6】——进阶实践(3)

    上一篇<Android自定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计划 ...

  4. Android自定义组件系列【5】——进阶实践(2)

    上一篇<Android自定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这一 ...

  5. Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动

    在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...

  6. Android 自定义组件之如何实现自定义组件

    参考链接:http://blog.csdn.net/jjwwmlp456/article/details/41076699 简介 Android提供了用于构建UI的强大的组件模型.两个基类:View和 ...

  7. Android自定义组件系列【3】——自定义ViewGroup实现侧滑

    有关自定义ViewGroup的文章已经很多了,我为什么写这篇文章,对于初学者或者对自定义组件比较生疏的朋友虽然可以拿来主义的用了,但是要一步一步的实现和了解其中的过程和原理才能真真脱离别人的代码,举一 ...

  8. Android 自定义组件,自定义LinearLayout,ListView等样式的组件

    今天讲的其实以前自己用过,就是在网上拿下来的把图片裁剪成圆形的方法,之前的随笔也介绍过的, 用法就是,在布局里写控件或者组件的时候得把从com开始到你写的那个类的所有路径写下来. 至于我们该怎么创建呢 ...

  9. Android自定义组件

    [参考的原文地址] http://blog.csdn.net/l1028386804/article/details/47101387效果图: 实现方式: 一:自定义一个含有EditText和Butt ...

随机推荐

  1. maven struts2工程StrutsPrepareAndExecuteFilter cannot be cast to javax.servlet.Filter

    maven搭建struts2工程时报错 严重: Exception starting filter struts2java.lang.ClassCastException: org.apache.st ...

  2. hibernate 模拟实现和What is and Why O/R Mapping

    What is and Why O/R Mapping What is : 用面向对象的方式调用api,类库帮我们翻译成面向关系的方式. Why: 1.JDBC操作数据库很繁琐2.Sql 语句编写并不 ...

  3. 257. Binary Tree Paths (dfs recurive & stack)

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  4. Selenium入门7 内嵌框架iframe

    如果网页内嵌iframe,那么iframe里的元素是无法直接定位的,需要使用switch_to.frame进入frame操作: 之后需要再操作页面上非嵌入在iframe里的元素,需要使用switch_ ...

  5. 将Apache2.4手动安装成Windows的服务

    将Apache2.4手动安装成Windows的服务 可以选择在安装Apache时自动将其安装为一个服务.如果选择"for all users",那么Apache将会被安装为服务. ...

  6. node执行环境

    nodejs本质上是一个javascript的执行环境,只是由于他的封装,加上更多web底层的一个处理,赋予了更多的能力,那么执行环境到底是什么呢,我们到浏览器里面体验看看,在chrome里面控制台, ...

  7. 2018.7.8 xmlhttp.readyState==4 && xmlhttp.status==200是什么意思

    在做DOM模型的XML实验的时候遇到了问题 代码实例: xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && ...

  8. python 添加 threadpool

    操作系统: Ubuntu 10.04 python安装依赖的软件包: python 出现 ImportError: No module named ** 我这里出现了: ImportError: No ...

  9. js判断移动端还是PC端

    function isMobile(){ var sUserAgent= navigator.userAgent.toLowerCase(), bIsIpad= sUserAgent.match(/i ...

  10. MySQL常见错误分析与解决方法总结

    MySQL常见错误分析与解决方法总结 一.Can't connect to MySQL server on 'localhost' (10061)翻译:不能连接到 localhost 上的mysql分 ...