在我们实际开发其中。会碰见一些布局结构类似或者同样的界面。比如应用的设置界面、tabbutton界面等。

这时候。对于刚開始学习的人来说,xml里面一个个绘制出来也许是最初的想法。可能随着经验的积累,又学会一招。就是使用include标签,导入类似或者同样的布局,提高了性能又降低了代码;再以后呢,自己定义控件又能够实现这一目的。本文就是简单的使用自己定义的组合控件模仿猫眼底部菜单条。

1.自己定义组合控件属性:在res/values文件夹下创建attrs.xml文件

    <declare-styleable name="TabItemView">
<attr name="contentTextSize" format="dimension"/> <!-- 字体大小 -->
<attr name="contentTextColor" format="color"/> <!-- 字体颜色 -->
<attr name="contentTextString" format="string"/> <!-- 显示的默认文字 -->
<attr name="contentLogoBack" format="reference"/> <!-- item背景 -->
<attr name="contentLogoSize" format="dimension" />
</declare-styleable>

TabItemView:简单一点说,就是属性组合的名字。

<attr />某个属性的定义:如<attr name="contentTextSize" format="dimension"/>,定义的就是文字的大小。contentTextSize指属性名字(和我们xml中经常使用的textSize)。format定义的是contentTextSize详细的属性类型。

以下简单的说下format的详细类型:

(1) reference:參考某一资源ID。 (2) color:颜色值。

(3) boolean:布尔值。 (4) dimension:尺寸值。

(5) float:浮点值。

(6) integer:整型值。

(7) string:字符串。 (8) fraction:百分数。

(9) enum:枚举值。 (10)flag:位或运算。

2.控件的属性定义完了,然后就是要在代码里面获取使用这些属性,看源码的时候。你会发现系统定义的属性都是通过TypedArray这玩意获取的,获取方法例如以下:

       TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.TabItemView);

以上方法返回的我们自己定义的属性集合,待最后用完之后,须要手动释放一下。ta.recycle();

TypedArray属性集合得到之后。以下就是依据须要获取不同的属性,针对不同的属性有不同的获取方法。以下是几个比較经常使用到的属性获取方法:

(1)getDimensionPixelSize:获取尺寸的大小(间距。文字大小等)

(2)getResourceId:获取资源id(图片等)

(3)getString:获取字符串

(4)getBoolean:获取布尔值

(5)getColor:获取颜色值

(6)getFloat:获取浮点类型值

以下是TabItemView自己定义组合控件的完整代码:

package com.dandy.weights;

import com.dandy.utils.PhoneUtils;
import com.demo.dandy.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.InflateException;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View.OnClickListener; public class TabItemView extends LinearLayout implements OnClickListener{ private Context mContext; private ImageView contentLogo;
private TextView contentText; private int logoBackResourceId;
private String textString;
private int textColor;
private float textSize;
private int contentLogoSize;
private static final float defaultTextSize = 16;
private int defaultColor,selectedColor;
private TabClickListner mClickListner; public TabItemView(Context context) {
this(context, null);
} public TabItemView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public TabItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext = context;
init(attrs);
addView();
} private void init(AttributeSet attrs){
this.setOnClickListener(this);
TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.TabItemView);
logoBackResourceId = ta.getResourceId(R.styleable.TabItemView_contentLogoBack, -1);
textColor = ta.getColor(R.styleable.TabItemView_contentTextColor, getResources().getColor(android.R.color.black));
textSize = ta.getDimensionPixelSize(R.styleable.TabItemView_contentTextSize, PhoneUtils.dp2px(mContext, defaultTextSize));
textString = ta.getString(R.styleable.TabItemView_contentTextString);
contentLogoSize = ta.getDimensionPixelSize(R.styleable.TabItemView_contentLogoSize, LayoutParams.WRAP_CONTENT);
ta.recycle();
defaultColor = mContext.getResources().getColor(R.color.textcolor_black_b3);
selectedColor = mContext.getResources().getColor(R.color.textcolor_red_d);
} private void addView(){
contentLogo = new ImageView(mContext);
contentLogo.setFocusable(false);
contentLogo.setClickable(false);
LayoutParams logoParams = new LayoutParams(contentLogoSize,contentLogoSize);
contentLogo.setLayoutParams(logoParams);
if(logoBackResourceId != -1){
contentLogo.setBackgroundResource(logoBackResourceId);
}else{
throw new InflateException("未设置填充图片资源");
} this.addView(contentLogo); if(!TextUtils.isEmpty(textString)){
contentText = new TextView(mContext);
contentText.setFocusable(false);
contentText.setClickable(false);
LayoutParams textParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
textParams.topMargin = PhoneUtils.dp2px(mContext,3);
contentText.setLayoutParams(textParams);
contentText.setTextColor(textColor);
contentText.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
contentText.setText(textString);
this.addView(contentText);
}
} @Override
public void onClick(View v) {
setTabSelected(true);
if(mClickListner != null){
mClickListner.onTabClick(this);
}
} /**
*设置点击监听事件
*/
public void setTabClickListener(TabClickListner listner){
this.mClickListner = listner;
} /**
*设置填充图片资源
*/
public void setContentLogoBack(int resourceId){
contentLogo.setBackgroundResource(resourceId);
} /**
*设置填充文字
*/
public void setContentTextString(String text){
if(contentText != null){
contentText.setText(text);
}
} /**
*设置选中状态
*/
public void setTabSelected(boolean enable){
if(contentLogo != null){
contentLogo.setSelected(enable);
}
if(contentText != null){
if(enable){
contentText.setTextColor(selectedColor);
}else{
contentText.setTextColor(defaultColor);
}
}
} public interface TabClickListner{
void onTabClick(View view);
} }

TabClickListener:控件点击的回调接口

3.控件搞定,然后就是在布局里面的详细使用

在res/layout/中创建tab_layout.xml文件,详细代码例如以下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tabItem="http://schemas.android.com/apk/res/com.demo.dandy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/background_bg7"
android:paddingTop="@dimen/tab_padding"
android:paddingBottom="@dimen/tab_padding"> <com.dandy.weights.TabItemView
android:id="@+id/movie"
style="@style/TabItemStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
tabItem:contentLogoBack="@drawable/selector_tab_movie"
tabItem:contentTextString="@string/movie" /> <com.dandy.weights.TabItemView
android:id="@+id/cinema"
style="@style/TabItemStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
tabItem:contentLogoBack="@drawable/selector_tab_cinema"
tabItem:contentTextString="@string/cinema" /> <com.dandy.weights.TabItemView
android:id="@+id/community"
style="@style/TabItemStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
tabItem:contentLogoBack="@drawable/selector_tab_community"
tabItem:contentTextString="@string/community" /> <com.dandy.weights.TabItemView
android:id="@+id/mine"
style="@style/TabItemStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
tabItem:contentLogoBack="@drawable/selector_tab_mine"
tabItem:contentTextString="@string/mine" /> </LinearLayout>

代码其中:xmlns:tabItem="http://schemas.android.com/apk/res/com.demo.dandy"。这段代码是关联你自己定义属性的。其中com.demo.dandy是指你应用的包名,tabItem是引用名

事实上就是拷贝xmlns:android="http://schemas.android.com/apk/res/android,替换一下android就ok了。

详细流程是:在构造函数中。获取TypedArray属性集合,然后获取所需的各个属性值,再通过动态加入控件ImageView和TextView,而且把对应定义的属性赋值给它们。

执行截图例如以下:

源码下载链接

android:自己定义组合控件Weight(高仿猫眼底部菜单条)的更多相关文章

  1. android 自己定义组合控件

    自己定义控件是一些android程序猿感觉非常难攻破的难点,起码对我来说是这种,可是我们能够在网上找一些好的博客关于自己定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲 ...

  2. [Android学习笔记]组合控件的使用

    组合控件的使用 开发过程中,多个UI控件需要协同工作,相互交互之后,才可完成一个完整的业务需求,此时可把这些控件封装成为一个整体,相互之间的交互逻辑封装其中,外部调用可无需关心内部逻辑,只需获取处理后 ...

  3. android 自定义空间 组合控件中 TextView 不支持drawableLeft属性

    android 自定义空间 组合控件中 TextView 不支持drawableLeft属性.会报错Caused by: android.view.InflateException: Binary X ...

  4. Android Studio自定义组合控件

    在Android的开发中,为了能够服用代码,会把有一定共有特点的控件组合在一起定义成一个自定义组合控件. 本文就详细讲述这一过程.虽然这样的View的组合有一个粒度的问题.粒度太大了无法复用,粒度太小 ...

  5. Android中自定义组合控件

    Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...

  6. android自己定义开关控件

    近日在android项目要使用开关控件.可是android中自带的开关控件不太惬意,所以就打算通过自己定义View写一个开关控件 ios的开关控件当然就是我要仿照的目标. 先上图:   waterma ...

  7. 撸一个Android高性能日历控件,高仿魅族

    Android原生的CalendarView根本无法满足我们日常开发的需要,在开发吾记APP的过程中,我觉得需要来一款高性能且美观简洁的日历控件,觉得魅族的日历风格十分适合,于是打算撸一款. gith ...

  8. android开发 获取父控件的高宽

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(wi ...

  9. Android自己定义View之组合控件 ---- LED数字时钟

    先上图 LEDView效果如图所看到的. 之前看到一篇博客使用两个TextView实现了该效果.于是我想用自己定义控件的方式实现一个LEDView.使用时就可以直接使用该控件. 採用组合控件的方式,将 ...

随机推荐

  1. 介绍一款好用 mongodb 可视化工具

    最近想自己搭建一个个人博客,所以学了下mongodb,mongodb是用命令行输入的,有些人可能不太习惯,我自己找了下mongodb的一些可视化工具,一开始安装的是mongoVUE,mongoVUE页 ...

  2. Android使用百度地图定位并显示手机位置后使用前置摄像头“偷拍”

    今天老板让我验证一下技术可行性,记录下来. 需求 :定位手机的位置并在百度地图上显示,得到位置后使用前置摄像头进行抓拍 拿到这个需求后,对于摄像头的使用不太熟悉,于是我先做了定位手机并在百度地图上显示 ...

  3. 通过扩大IE使用内存,解决skyline在IE下模型不能加载的方法

    环境:skyline TerraExploere 6.6.1,win10 专业版 64位,ie 11 情况描述:在ie下浏览三维场景,ie占用内存不断增大并且内存占用固定在一个最高范围内,三维场景中部 ...

  4. linux操作系统基础篇(九)

    shell脚本的运算符与流程控制 1.运算符 1.1 算术运算符 + - * / % [root@MiWiFi-R3-srv ~]# echo $[3+1]4 1.2 关系操作 与(())连用 < ...

  5. innobackupex: fatal error: no ‘innodb_buffer_pool_filename’解决方法

    http://www.ttlsa.com/mysql/innobackupex-1-5-1-fatal-error-no-innodb_buffer_pool_filename/

  6. [转]ORACLE SQL解析之硬解析和软解析

    http://blog.chinaunix.net/uid-25909722-id-3363789.html 当客户端进程,将SQL语句通过监听器发送到Oracle时, 会触发一个Server pro ...

  7. 网页如何展示PPT文档

    最近再做一个新项目,其中有一个难点,就是如何在网页上展示PPT,我网上找到了几种方法,但是真正符合我目前这个项目的就只有这一种方法了,       使用PowerPoint to Flash将ppt文 ...

  8. GridControl使用技巧总结,更新中...

    1如何禁用GridControl中单击列弹出右键菜单 设置Run Design->OptionsMenu->EnableColumnMenu 设置为:false 2如何定位到第一条数据/记 ...

  9. iOS打包静态库(完整篇)

    1. 什么是库? 所谓库就是程序代码的集合,是共享程序代码的一种方式. 2. 库的分类 根据程序代码的开源情况,库可以分为两类 开源库源代码是公开的,你可以看到具体实现.比如GitHub上比较出名的第 ...

  10. Redis 图形化监控方案 RedisLive 介绍

    作为一款开源的 Redis 图形化监控工具,RedisLive 提供对 Redis 实例的内存使用情况,接收的客户端命令,接收的请求数量以及键进行监控.RedisLive 的工作原理基于 Redis ...