安卓系统为我们提供了丰富的控件,但是在实际项目中我们仍然需要重新通过布局来实现一些效果,比如我们需要一个上面图标,下面文字的button,类似于下面这样的:

最直接的解决办法是通过将imageview和textview放在一个垂直排列的LinearLayout中,如下:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center"

android:orientation="vertical" >

android:id="@+id/icon_part"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

/>

android:id="@+id/text_part"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:textColor="#000000" />

但是每一个button都需要这么长的代码,上面三个按钮的话就需要重复写三次,而且别人一看是个LinearLayout,不会将它button联系起来。

如果有一种办法能将上面那个布局组合成一个控件就好了。

的确是有办法的。主要有两方面的工作。

1.新建一个继承自LinearLayout的类(也可以是其他布局类,不过LinearLayout好像比较合适),然后通过inflater在这个类的构造函数中将上面的布局添加进去。

2.为了能在xml中也给这个自定义控件赋予属性来获得现实效果,比如字体大小、图标资源等,我们还需要在attrs文件中申明一些自定义属性。你可以查阅declare-styleable了解这是怎么回事。

我这里有一个已经实现了这种button效果的类FlexImageButton:package com.jcodecraeer.client.widget;

import com.jcodecraeer.client.R;

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 FlexImageButton extends LinearLayout {

private ImageView imageView;

private TextView textView;

private CharSequence text;

private Drawable drawable;

private float textSize;

public FlexImageButton(Context context) {

super(context);

// TODO Auto-generated constructor stub

}

public FlexImageButton(Context context, AttributeSet attrs) {

super(context, attrs);

// TODO Auto-generated constructor stub

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FlexImageButton);

text = a.getText(R.styleable.FlexImageButton_text);

if(text==null){

text="";

}

Drawable d = a.getDrawable(R.styleable.FlexImageButton_src);

if (d != null) {

drawable=d;

} else {

throw new RuntimeException("图像资源为空");

}

textSize = a.getDimension(R.styleable.FlexImageButton_textSize,12);

String infService = Context.LAYOUT_INFLATER_SERVICE;

LayoutInflater inflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

inflater.inflate(R.layout.flex_image_button_layout, this);

imageView = (ImageView) findViewById(R.id.icon_part);

imageView.setImageDrawable(drawable);

textView = (TextView) findViewById(R.id.text_part);

textView.setTextSize((float) textSize);

textView.setText(text);

if(text.equals("")||text==null){

textView.setVisibility(View.GONE);

}

a.recycle();

}

public void setImageResource(int resId) {

imageView.setImageResource(resId);

}

public void setTextViewText(String text) {

textView.setText(text);

}

}

在attrs.xml文件中我们声明一些自定义属性,这里我们希望我的FlexImageButton能拥有可以灵活设置的文字属性,字体大小属性、图标资源属性,因此我这样定义:

其中format="reference"表示这个属性的值类型是资源id,也就是说在使用FlexImageButton的时候我只可以用资源id来为这个属性赋值。属性值类型有那些,我在文章结尾的附录里面一一列出。

上面我们已经完成了一个自定义的控件,activity的布局文件中如下使用FlexImageButton:

android:layout_height="fill_parent"

android:layout_width="50dip"

cl:src="@drawable/toolbar_collect"

cl:text="@string/collect"

android:clickable="true"

android:focusable="true"

android:layout_marginLeft="5dip"

android:layout_marginRight="5dip"

android:contentDescription="收藏"

android:background="@drawable/back_selector"

/>

仔细的人会注意到所有这些属性中,有两种形式。其中凡是android:开头的都是系统属性,而cl:src="@drawable/toolbar_collect"

cl:text="@string/collect"

为我自定义的属性,为什么是cl:开头?

你也可以不用cl开头,但是不管你用什么开头,如果你用到了自定义属性,你都必须在activity布局文件的最开始这样声明:<?xml version="1.0" encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:cl="http://schemas.android.com/apk/res/com.jcodecraeer.client"

其中cl就是刚刚用到的,com.jcodecraeer.client为我的apk包名,注意是apk包名,而不是你自定义控件的在包中的路径。

附录:自定义属性的值类型:

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

(1)属性定义:

(2)属性使用:

android:layout_width="42dip"

android:layout_height="42dip"

android:background="@drawable/图片ID" />

2. color:颜色值。

(1)属性定义:

(2)属性使用:

android:layout_width="42dip"

android:layout_height="42dip"

android:textColor="#00FF00" />

3. boolean:布尔值。

(1)属性定义:

(2)属性使用:

android:layout_width="42dip"

android:layout_height="42dip"

android:focusable="true" />

4. dimension:尺寸值。

(1)属性定义:

(2)属性使用:

android:layout_width="42dip"

android:layout_height="42dip" />

5. float:浮点值。

(1)属性定义:

(2)属性使用:

android:fromAlpha="1.0"

android:toAlpha="0.7" />

6. integer:整型值。

(1)属性定义:

(2)属性使用:

android:frameDuration="100"

android:framesCount="12"

/>

7. string:字符串。

(1)属性定义:

(2)属性使用:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" />

8. fraction:百分数。

(1)属性定义:

(2)属性使用:

android:pivotX="200%"

android:pivotY="300%"

/>

9. enum:枚举值。

(1)属性定义:

(2)属性使用:

android:orientation="vertical" >

10. flag:位或运算。

(1)属性定义:

(2)属性使用:

android:windowSoftInputMode="stateUnspecified | stateUnchanged | stateHidden" >

注意:属性定义时可以指定多种类型值:

(1)属性定义:

(2)属性使用:

android:layout_width="42dip"

android:layout_height="42dip"

android:background="@drawable/图片ID|#00FF00" />

android自定义组件属性,android自定义控件并添加属性的方法以及示例相关推荐

  1. Android自定义View实现三角到八角的属性分布图-雷达图(蜘蛛网图)

    Android自定义View实现三角到八角的属性分布图-雷达图(蜘蛛网图) 前言 自定义View的关键点 绘制多边形 结尾 前言 刚开始学习自定义view,简单完成了一个属性分布器,可以实现三条到八条 ...

  2. android 自定义输入法布局,Android自定义输入法使用方法

    Android自定义输入法使用方法 时间:2017-04-21     来源:Android开发学习网 对于Android用户而言,一般都会使用第三方的输入法.可是在输入密码时(尤其是支付相关的密码) ...

  3. android 自定义dialog样式,Android 自定义dialog类

    首先定制style样式 styles.xml 加入自定义样式 @null true true true @color/transparent @color/transparent true 0.6 在 ...

  4. android自定义组件属性,Android组合控件详解 自定义属性

    组合控件详解 & 自定义属性 组合控件是自定义控件的一种,只不过它是由其他几个原生控件组合而成,故名组合控件. 在实际项目中,GUI 会遇到一些可以提取出来做成自定义控件情况. 一个自定义控件 ...

  5. Android自定义组件之ListPopWindow

    最近小编在学习IOS开发,感触颇深,看到了iOS里面封装了好多组件,很多组件都是iOS自带的,相信一般的小公司的产品经理都是按照iOS的交互来设计UI,而且还要求Android要和iOS统一风格,这让 ...

  6. android 自定义布局 attribute·,android 自定义控件之xml---- attributeset attrs

    主要是说明android 自定义控件的在布局代码中设置属性,如何自定义attributeset ,和在控件中如何获取到xml布局中设置的属性值,不是想了解这方面的请略过...不浪费大家时间 1.首先添 ...

  7. Android 自定义组件随着手指自动画圆

    首先自定义一个View子类: package com.example.androidtest0.myView;import android.content.Context; import androi ...

  8. android自定义刻度线,Android自定义控件之刻度尺控件

    今天我做的是一个自定义刻度尺控件,由于项目需求需要使用刻度尺那样滑动选择,由于对自定义控件的认识还不够深入,于是花了一周多时间才把这个控件给整出来,也是呕心沥血的经历啊,也让我对自定义控件有了自己的认 ...

  9. Android自定义组件——3D立体旋转控件

    BingoIdea 一个3D立体切换的自定义ViewGroup demo,支持横向和纵向滑动翻转. 项目配置 在项目的build.gradle配置 allprojects {repositories ...

最新文章

  1. [svc]samba服务搭建
  2. Follow Me:CCIE RS--新版CCIE Routing Switching 考纲要点
  3. 大龄计算机考研 考研帮,大龄学子考研之路
  4. IBatis.Net学习笔记二--下载、编译、运行NPetShop
  5. 手把手教你启用Win10的Linux子系统 Ubuntu
  6. 添加一列_Joom平台CSV文件如何添加产品?CSV文件添加产品流程一览
  7. 入行十年,总结出了数据仓库、数据集市、数据库的精华,你一定不能错过
  8. Hibernate 一对一关联查询
  9. 【AC】九度OJ题目1153:括号匹配问题
  10. Vuejs --01 起步
  11. 360全景拼接 opencv_全景拼接算法简介
  12. ITIL事件管理流程设计
  13. python-numpy常用知识汇总
  14. Java可以做什么工作
  15. 网络安全:IP地址定位方式
  16. iOS 苹果登录(第三方登录)
  17. oracle数据库迁移-TTS迁移操作案例01
  18. 构建最小根文件系统lfs
  19. 百度直达号,一场自high的喜剧
  20. 96Boards MIPI CSI Camera Mezzanine

热门文章

  1. 【01】Clean Code
  2. vc 国际化的资源文件处理
  3. [Overleaf] -带你初识Latex神器.快速上手.更新中...
  4. Python Django后台管理模板美化:使用django-simpleui模块
  5. golang基本数据类型和string的相互转换
  6. vsphere vcenter安装
  7. 【辟谣】java中的final方法在新版的jvm中能提高效率?
  8. 【Java面试题视频讲解】提取不重复的整数
  9. java中的重写与重载_java中的重写与重载
  10. 多边形之间相交求交点的算法_路径规划算法总结